Skip to content

Commit

Permalink
Make sure just_run doesn't leak file descriptors of new loops.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariokostelac committed Jun 28, 2022
1 parent 9a877a1 commit 818f2a7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
16 changes: 16 additions & 0 deletions nbclient/tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import unittest.mock
from unittest.mock import MagicMock

import psutil
Expand Down Expand Up @@ -94,3 +95,18 @@ async def async_sleep():
just_run(async_sleep())
diff.append(proc.num_fds() - fds_count)
assert diff == [0] * 10


def test_just_run_clears_new_loop():
async def async_sleep():
await asyncio.sleep(0.1)

loop = asyncio.new_event_loop()
loop.stop = MagicMock(wraps=loop.stop)
loop.close = MagicMock(wraps=loop.close)

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

loop.stop.assert_called_once
loop.close.assert_called_once
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

0 comments on commit 818f2a7

Please sign in to comment.