Skip to content

Commit e2e36b7

Browse files
committed
Fix asyncio error when opening notebooks
This is a fix for jupyter/notebook#6164 `nest_asyncio` must be applied before any async tasks have been created otherwise there could be tasks queued that are unpatched, and thus do not respect nested loops. An example of an unpatched task can be seen in the original issue: ``` <Task pending coro=<HTTP1ServerConnection._server_request_loop() running at /apps/python3/lib/python3.7/site-packages/tornado/http1connection.py:823> ``` which originates from Tornado. A similar issue was reported in `nest-asyncio`: erdewit/nest_asyncio#22 where the solution is to call `apply` on import so that unpatched tasks do not get created.
1 parent bbbb495 commit e2e36b7

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

notebook/tests/launchnotebook.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,16 @@ def start_thread():
167167
token=cls.token,
168168
**bind_args
169169
)
170-
if 'asyncio' in sys.modules:
171-
app._init_asyncio_patch()
172-
import asyncio
173-
asyncio.set_event_loop(asyncio.new_event_loop())
170+
if "asyncio" in sys.modules:
171+
app._init_asyncio_patch()
172+
import asyncio
173+
174+
asyncio.set_event_loop(asyncio.new_event_loop())
175+
# Patch the current loop in order to match production
176+
# behavior
177+
import nest_asyncio
178+
179+
nest_asyncio.apply()
174180
# don't register signal handler during tests
175181
app.init_signal = lambda : None
176182
# clear log handlers and propagate to root for nose to capture it

0 commit comments

Comments
 (0)