-
Notifications
You must be signed in to change notification settings - Fork 288
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
KernelProvisionerBase
(and all classes derived from it) runs its async functions in different eventloops
#959
Comments
Thanks for opening this issue @dchirikov - this does seem like a problem. Are you trying to extend I have not experienced this kind of issue with any of the Unfortunately, I am not aware of what implicitly (?) triggers the use of a different event loop. I'm hoping others like @blink1073 or @davidbrochart might have ideas on where/how a different loop may be getting introduced in the kernel's lifecycle. |
Hi @kevin-bates ! Nice to see you here! You helped me with Enterprise Gateway issue while ago. Yes, I am trying to implement a custom provisioner based on In a meanwhile I was trying to pinpoint where issue arises and noticed another symptom. If I run So it it looks like treads are messing up with ioloops not in a good way. |
Looking at the trace output from above, it seems like the difference between the contexts (i.e., event loop values) of when those particular methods are called relate to the introduction of the kernel messaging (as well as the Popen instance from within the provisioner). That is, the ioloop value
Again, I'll have to defer to folks more expert than myself. |
I brought this topic up in today's Server/Kernel's meeting and @vidartf proposed using a custom event-loop policy to gain more detailed troubleshooting analysis. It was also suggested that the version of |
Brilliant idea about custom policy. Here is what I did. I patched the
Here is a full output of kernel run: I am using Hope it will help. |
I guess, the root cause of the issue is somewhere here: https://github.com/jupyter/jupyter_client/blob/main/jupyter_client/kernelapp.py So a And then |
Hi @dchirikov, thanks for the thorough analysis! Yes, it seems as if it is unsafe to instantiate a I think we need to provide an API in |
@dchirikov, actually, this might be enough of a fix, if you can verify: diff --git a/jupyter_core/utils/__init__.py b/jupyter_core/utils/__init__.py
index 68973e6..c3cb38f 100644
--- a/jupyter_core/utils/__init__.py
+++ b/jupyter_core/utils/__init__.py
@@ -126,7 +126,6 @@ class _TaskRunner:
_runner_map = {}
-_loop_map = {}
def run_sync(coro: Callable[..., Awaitable[T]]) -> Callable[..., T]:
@@ -160,9 +159,11 @@ def run_sync(coro: Callable[..., Awaitable[T]]) -> Callable[..., T]:
pass
# Run the loop for this thread.
- if name not in _loop_map:
- _loop_map[name] = asyncio.new_event_loop()
- loop = _loop_map[name]
+ try:
+ loop = asyncio.get_event_loop()
+ except RuntimeError:
+ loop = asyncio.new_event_loop()
+ asyncio.set_event_loop(loop)
return loop.run_until_complete(inner)
wrapped.__doc__ = coro.__doc__ |
@blink1073 Please tell me where to patch, jupyter_core or jupyter_client ? |
Hi, I am trying to build customer Kernel Provisioner and and build my own class to handle kernel's lifecycle. As almost all the function in that class are marked as asynchronous I assumed there would no issues to have a class with asynchronous functions and tasks to handle communications with the kernel. Turned out it was not that straightforward.
And the reason is that different functions in
KernelProvisionerBase
are being run different event loops. Which undermines the whole idea. In my case my tasks are being blocked forever.I noticed that
pre_launch
,launch_kernel
andpost_launch
are run in one event loop and all otherspoll
,wait
,kill
,terminate
in other one.To illustrate that here is a small patch for
jupyter_client/provisioning/local_provisioner.py
which display's the IDs of event loops calls are running in:The example of
jupyter-kernel --debug --kernel python
command output:How to tackle that? Can I do anything to stick my provisioner to single event loop?
The text was updated successfully, but these errors were encountered: