Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent file descriptor leak in util.just_run #237

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion nbclient/tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import asyncio
import unittest.mock
from unittest.mock import MagicMock

import psutil # type: ignore
import pytest
import tornado

from nbclient.util import run_hook, run_sync
from nbclient.util import just_run, run_hook, run_sync


@run_sync
Expand Down Expand Up @@ -75,3 +77,27 @@ async def test_run_hook_async():
hook = MagicMock(return_value=some_async_function())
await run_hook(hook)
assert hook.call_count == 1


def test_just_run_doesnt_leak_fds():
proc = psutil.Process()

# Warmup, just to make sure we're not failing on some initial fds being opened for the first time.
for _ in range(10):
just_run(asyncio.sleep(0.01))
fds_count = proc.num_fds()

diff = []
for _ in range(10):
just_run(asyncio.sleep(0.01))
diff.append(proc.num_fds() - fds_count)
assert diff == [0] * 10


def test_just_run_clears_new_loop():
loop = asyncio.new_event_loop()

with unittest.mock.patch.object(asyncio, "new_event_loop", return_value=loop):
just_run(asyncio.sleep(0.01))

assert loop.is_closed()
6 changes: 5 additions & 1 deletion nbclient/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ def just_run(coro: Awaitable) -> Any:

nest_asyncio.apply()
check_patch_tornado()
return loop.run_until_complete(coro)
res = loop.run_until_complete(coro)
if not had_running_loop:
loop.stop()
loop.close()
return res


T = TypeVar("T")
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ipywidgets<8.0.0
mypy
pip>=18.1
pre-commit
psutil
pytest>=4.1
pytest-asyncio
pytest-cov>=2.6.1
Expand Down