-
Notifications
You must be signed in to change notification settings - Fork 66
Prevent file descriptor leak in util.just_run #237
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
9a877a1
818f2a7
47283dc
fbf02e9
11fea9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||||||||||
| import pytest | ||||||||||
| import tornado | ||||||||||
|
|
||||||||||
| from nbclient.util import run_hook, run_sync | ||||||||||
| from nbclient.util import just_run, run_hook, run_sync | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @run_sync | ||||||||||
|
|
@@ -75,3 +77,36 @@ 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() | ||||||||||
|
|
||||||||||
| async def async_sleep(): | ||||||||||
| await asyncio.sleep(0.01) | ||||||||||
|
|
||||||||||
| # 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(async_sleep()) | ||||||||||
| fds_count = proc.num_fds() | ||||||||||
|
|
||||||||||
| diff = [] | ||||||||||
| for _ in range(10): | ||||||||||
| 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) | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's just ignore type checks on these.
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I might have a better solution for these, without ignoring type checks. I can't run tests locally, though.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're not using
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I see, I was running Re: type checking. Would it make sense to put it as pre-commit check?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Indeed!
Definitely, if you want to open a PR for that, it would be great. |
||||||||||
|
|
||||||||||
| 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 | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.