Skip to content

Commit

Permalink
Fix asyncio error when opening notebooks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dleen committed Nov 3, 2021
1 parent bbbb495 commit e2e36b7
Showing 1 changed file with 10 additions and 4 deletions.
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

0 comments on commit e2e36b7

Please sign in to comment.