Skip to content

Commit 7a18189

Browse files
Make kernelbase._eventloop_set event thread-safe (#1366)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 700c26f commit 7a18189

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

ipykernel/kernelapp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import zmq
2222
import zmq.asyncio
23-
from anyio import create_task_group, run
23+
from anyio import create_task_group, run, to_thread
2424
from IPython.core.application import ( # type:ignore[attr-defined]
2525
BaseIPythonApplication,
2626
base_aliases,
@@ -791,7 +791,7 @@ def start(self) -> None:
791791
run(self._start, backend=backend)
792792

793793
async def _wait_to_enter_eventloop(self) -> None:
794-
await self.kernel._eventloop_set.wait()
794+
await to_thread.run_sync(self.kernel._eventloop_set.wait)
795795
await self.kernel.enter_eventloop()
796796

797797
async def main(self) -> None:

ipykernel/kernelbase.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
import psutil
3838
import zmq
39-
from anyio import TASK_STATUS_IGNORED, Event, create_task_group, sleep, to_thread
39+
from anyio import TASK_STATUS_IGNORED, create_task_group, sleep, to_thread
4040
from anyio.abc import TaskStatus
4141
from IPython.core.error import StdinNotImplementedError
4242
from jupyter_client.session import Session
@@ -235,12 +235,14 @@ def _parent_header(self):
235235
"list_subshell_request",
236236
]
237237

238-
_eventloop_set: Event = Event()
238+
_eventloop_set: threading.Event
239239

240240
def __init__(self, **kwargs):
241241
"""Initialize the kernel."""
242242
super().__init__(**kwargs)
243243

244+
self._eventloop_set = threading.Event()
245+
244246
# Kernel application may swap stdout and stderr to OutStream,
245247
# which is the case in `IPKernelApp.init_io`, hence `sys.stdout`
246248
# can already by different from TextIO at initialization time.

0 commit comments

Comments
 (0)