-
Notifications
You must be signed in to change notification settings - Fork 56
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
base: main
Are you sure you want to change the base?
Conversation
c87ce65
to
818f2a7
Compare
I see some type checks are failing. I'll probably need help to get around it. I've tried using MagicMock to swap loop object, but some code asserts that loop objects is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot @mariokostelac, that looks good.
nbclient/tests/test_util.py
Outdated
loop.stop = MagicMock(wraps=loop.stop) | ||
loop.close = MagicMock(wraps=loop.close) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just ignore type checks on these.
loop.stop = MagicMock(wraps=loop.stop) | |
loop.close = MagicMock(wraps=loop.close) | |
loop.stop = MagicMock(wraps=loop.stop) # type: ignore | |
loop.close = MagicMock(wraps=loop.close) # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
(venv) dev-envs-ad\mario@a-2zurb4h4bbny ➜ nbclient git:(mario/fix_fd_leak) ls
binder CHANGELOG.md CONTRIBUTING.md docs LICENSE MANIFEST.in nbclient nbclient.egg-info pyproject.toml pytest.ini README.md RELEASING.md requirements-dev.txt requirements.txt setup.cfg setup.py venv
(venv) dev-envs-ad\mario@a-2zurb4h4bbny ➜ nbclient git:(mario/fix_fd_leak) tox -e py38
ERROR: tox config file (either pyproject.toml, tox.ini, setup.cfg) not found
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're not using tox
, you should just run pytest
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see, I was running pytest
, but it was missing the type checking piece that failed. I think https://github.com/jupyter/nbclient/blob/main/CONTRIBUTING.md might need an update.
Re: type checking. Would it make sense to put it as pre-commit check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see, I was running
pytest
, but it was missing the type checking piece that failed. I think https://github.com/jupyter/nbclient/blob/main/CONTRIBUTING.md might need an update.
Indeed!
Re: type checking. Would it make sense to put it as pre-commit check?
Definitely, if you want to open a PR for that, it would be great.
Co-authored-by: David Brochart <david.brochart@gmail.com>
@davidbrochart I think this is in better shape now. |
Trigger CI. |
This is interesting. It seems that something in It's also strange that the function I'm changing looks remarkably similar to the one that's in the stacktrace. Why is that? @davidbrochart I see that you've authored both of them (at least parts). I'll do some debugging tomorrow or on Friday. If you have some context that might help, I'm all ears. Also sorry for not running the whole testsuite and delaying this bug discovery till CI run. |
@davidbrochart there's some discussion in jupyter/jupyter_client#772 (comment). |
I've tried hiding our loop from Unfortunately, nbclient/nbclient/tests/test_client.py Line 488 in 33b6e5f
kc.shutdown eventually tries to send something to control channel, which expect some running loop.I'm not 100% sure I can locate the bug. Is the bug in the test, running kc.shutdown outside of the loop? |
I've spent more time investigating this and I think this solution might be dangerous. It's changing the "current loop", and then stopping it. jupyter_client expects that the loop returned by There are several approaches we could take:
Happy to hear your thoughts on these approaches, or new approaches entirely. |
I like the idea of using |
While working with papermill I've found that repeated running of notebooks is eventually bumping into the limit of number of opened files.
Following he stacktrace led me to
util.just_run
function that creates asyncio loop in some cases, but doesn't close them, which leaves someeventpoll
file descriptors opened forever.This PR consists of two commits
just_run
call. It also explicitly declarespsutil
as a dependency. It was already a transitive dependency, but I'm promoting to be explicit since I'm calling it directly.Happy to discuss the approach, missing context, and next steps to fix this bug.