jupyter-server / jupyter_server

The backend—i.e. core services, APIs, and REST endpoints—to Jupyter web applications.
https://jupyter-server.readthedocs.io
BSD 3-Clause "New" or "Revised" License
488 stars 306 forks source link

Slower atexit methods do not run to completion #1422

Open parantapa opened 6 months ago

parantapa commented 6 months ago

Description

Cleanup methods that are slower do not run to completion when restarting kernel.

Reproduce

  1. Create a new IPython notebook in Jupyter Lab.
  2. Create and execute a new cell with the following code
import atexit
import time
from pathlib import Path

def write_hello():
    time.sleep(5)
    Path("hello.txt").write_text("hello")

atexit.register(write_hello)
  1. Click on "Restart kernel"
  2. The new file "hello.txt" was not created.

Expected behavior

The "hello.txt" should have been created.

Context

Additional info

Jupyter lab shows a TimeoutError on console.

[E 2024-05-02 10:50:31.388 ServerApp] Uncaught exception GET /api/kernels/28d27336-01eb-4fed-9b24-e5b4a1ae8ef4/channels?session_id=3fa3bd68-c341-4029-b71b-92c68bdc3ba6 (127.0.0.1)
    HTTPServerRequest(protocol='http', host='localhost:8888', method='GET', uri='/api/kernels/28d27336-01eb-4fed-9b24-e5b4a1ae8ef4/channels?session_id=3fa3bd68-c341-4029-b71b-92c68bdc3ba6', version='HTTP/1.1', remote_ip='127.0.0.1')
    Traceback (most recent call last):
      File "/home/parantapa/miniconda3/envs/notebook_env/lib/python3.11/site-packages/tornado/web.py", line 1790, in _execute
        result = await result
                 ^^^^^^^^^^^^
      File "/home/parantapa/miniconda3/envs/notebook_env/lib/python3.11/site-packages/jupyter_server/services/kernels/websocket.py", line 65, in get
        await self.pre_get()
      File "/home/parantapa/miniconda3/envs/notebook_env/lib/python3.11/site-packages/jupyter_server/services/kernels/websocket.py", line 59, in pre_get
        await self.connection.prepare()
      File "/home/parantapa/miniconda3/envs/notebook_env/lib/python3.11/site-packages/jupyter_server/services/kernels/connection/channels.py", line 318, in prepare
        raise TimeoutError(msg)
    TimeoutError: Kernel never reached an 'alive' state.

This error is gone when using a larger kernel_info_timeout. However, the atexit method still does not run to completion.

The above code works as expected if run from IPython (inside shell).

I have been trying to find the spot in Jupyter Lab, Jupyter Server and Ipython that is responsible for killing the kernel. But I since I am not familiar enough, I haven't figured it out.

I had posted this issue on JupyterLab https://github.com/jupyterlab/jupyterlab/issues/16276#issue-2275760074 and was told this might be a better place to report the issue.

welcome[bot] commented 6 months ago

Thank you for opening your first issue in this project! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out Jupyter's Code of Conduct. Also, please try to follow the issue template as it helps other other community members to contribute more effectively. welcome You can meet the other Jovyans by joining our Discourse forum. There is also an intro thread there where you can stop by and say Hi! :wave:
Welcome to the Jupyter community! :tada:

parantapa commented 4 months ago

Bump.

parantapa commented 1 month ago

Bump.