ipython / ipython/ipykernel

Strange occasional `RuntimeError: Event loop is closed` out of nowhere

オープン
#1,155 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Python
スター
734
フォーク
412
平均マージ
1日 5時間
マージ済み PR(30日)
8

説明

I'm implementing a kernel by inheriting from `ipykernel.kernelbase.Kernel`, as described at [Making simple Python wrapper kernels](https://jupyter-client.readthedocs.io/en/latest/wrapperkernels.html).

That generally works well, but I have a strange problem: Occasionally I get an exception `RuntimeError: Event loop is closed`:

````
Traceback (most recent call last):
File "/home/ca/Store/micromamba/envs/std/lib/python3.10/site-packages/ipykernel/iostream.py", line 390, in _watch_pipe_fd
self.write(bts.decode(errors='replace'))
File "/home/ca/Store/micromamba/envs/std/lib/python3.10/site-packages/ipykernel/iostream.py", line 664, in write
self._schedule_flush()
File "/home/ca/Store/micromamba/envs/std/lib/python3.10/site-packages/ipykernel/iostream.py", line 561, in _schedule_flush
self.pub_thread.schedule(_schedule_in_thread)
File "/home/ca/Store/micromamba/envs/std/lib/python3.10/site-packages/ipykernel/iostream.py", line 269, in schedule
f()
File "/home/ca/Store/micromamba/envs/std/lib/python3.10/site-packages/ipykernel/iostream.py", line 559, in _schedule_in_thread
self._io_loop.call_later(self.flush_interval, self._flush)
File "/home/ca/Store/micromamba/envs/std/lib/python3.10/site-packages/tornado/ioloop.py", line 597, in call_later
return self.call_at(self.time() + delay, callback, *args, **kwargs)
File "/home/ca/Store/micromamba/envs/std/lib/python3.10/site-packages/tornado/platform/asyncio.py", line 206, in call_at
return self.asyncio_loop.call_later(
File "/home/ca/Store/micromamba/envs/std/lib/python3.10/asyncio/base_events.py", line 721, in call_later
timer = self.call_at(self.time() + delay, callback, *args,
File "/home/ca/Store/micromamba/envs/std/lib/python3.10/asyncio/base_events.py", line 732, in call_at
self._check_closed()
File "/home/ca/Store/micromamba/envs/std/lib/python3.10/asyncio/base_events.py", line 515, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
````

The traceback does not go back to my code, so I'm not certain whether I did something wrong, or there is a bug in `ipykernel.kernelbase`? I am unable to reproduce the error, in the exact same situation it sometimes happens and sometimes doesn't.

I found two other issues which mention this exception, but they are about UI events (#825) or parallelization (#534), both of which I do not use. I inherited from `ipykernel.kernelbase.Kernel` and implemented `do_execute`, which sends code to an external interpreter and sends its stdout and stderr via `self.send_response`.

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

Start with ipykernel/iostream.py, especially _watch_pipe_fd, write, _schedule_flush, and the pub-thread scheduling path shown in the traceback. Compare that lifecycle with Kernel.do_execute and the external interpreter's stdout and stderr handling, then investigate when the asyncio loop is closed. Done means identifying a reproducible shutdown race or confirming the user code responsible and adding a regression test if the issue is in ipykernel.

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend, devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。