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

Fix asyncio error when opening notebooks #6221

Merged
merged 1 commit into from
Nov 16, 2021
Merged
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
27 changes: 27 additions & 0 deletions notebook/notebookapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2092,7 +2092,34 @@ def _init_asyncio_patch(self):

FIXME: if/when tornado supports the defaults in asyncio,
remove and bump tornado requirement for py38

With the introduction of the async kernel, the existing sync kernel
requires the use of nested loops in order to run code synchronously.
This is done in `jupyter_client` using the helper util `run_sync`:

ref: https://github.com/jupyter/jupyter_client/blob/f453b51eeeff9e905c583b7da3905c0e35cfbdf0/jupyter_client/utils.py#L11

which creates a new event loop and relies on `nest_asyncio` patching
to allow nested loops. This requires that *all* potential tasks are
patched before executing. When only some tasks are patched it leads to
the following issue:

ref: https://github.com/jupyter/notebook/issues/6164

So we must call `nest_asyncio.apply()` method as early as possible. It
is preferable to do this in the consuming application rather than the
`jupyter_client` as it is a global patch and would impact all consumers
rather than just the ones that rely on synchronous kernel behavior.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice comment - thank you.

"""
import nest_asyncio

try:
nest_asyncio.apply()
except RuntimeError:
# nest_asyncio requires a running loop in order to patch.
# In tests the loop may not have been created yet.
pass

if sys.platform.startswith("win") and sys.version_info >= (3, 8):
import asyncio
try:
Expand Down
14 changes: 10 additions & 4 deletions notebook/tests/launchnotebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,16 @@ def start_thread():
token=cls.token,
**bind_args
)
if 'asyncio' in sys.modules:
app._init_asyncio_patch()
import asyncio
asyncio.set_event_loop(asyncio.new_event_loop())
if "asyncio" in sys.modules:
app._init_asyncio_patch()
import asyncio

asyncio.set_event_loop(asyncio.new_event_loop())
# Patch the current loop in order to match production
# behavior
import nest_asyncio

nest_asyncio.apply()
# don't register signal handler during tests
app.init_signal = lambda : None
# clear log handlers and propagate to root for nose to capture it
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
'jupyter_client>=5.3.4',
'nbformat',
'nbconvert',
'nest-asyncio>=1.5',
'ipykernel', # bless IPython kernel for now
'Send2Trash>=1.8.0',
'terminado>=0.8.3',
Expand Down