datalayer / jupyter-server-nbmodel

Jupyter Server NbModel. - Execute code cell from the server.
https://datalayer.tech
BSD 3-Clause "New" or "Revised" License
13 stars 2 forks source link

When worker stops the API keeps returning `202` #27

Open krassowski opened 2 months ago

krassowski commented 2 months ago

Description

When worker stops due to an error the API will keep returning 202 rather than show an error. This means that the client will wait for execution in an infinite loop

Reproduce

one way to reproduce it is to programatically stop the pycrdt_websocket server:

[D 2024-08-27 09:36:09.061 ServerApp.jupyter_server_nb_model] Starting worker to process execution requests of kernel b9e05053-c3c9-4865-861f-77bac0f548c6…
[D 2024-08-27 09:36:09.061 ServerApp.jupyter_server_nb_model] Processing execution request a6f69c19-1435-4616-91d6-547e25c722d2 for kernel b9e05053-c3c9-4865-861f-77bac0f548c6…
[D 2024-08-27 09:36:09.061 ServerApp.jupyter_server_nb_model] a6f69c19-1435-4616-91d6-547e25c722d2  {'document_id': 'json:notebook:e70c9a57-dfa3-45ff-9509-88fccda8da2a', 'cell_id': '885916f4-16de-4c00-9873-56795ba1734f'}
[D 2024-08-27 09:36:09.061 ServerApp.jupyter_server_nb_model] Stopping execution requests worker for kernel b9e05053-c3c9-4865-861f-77bac0f548c6…
    Traceback (most recent call last):
      File "/home/jupyter/notebook-env/lib/python3.11/site-packages/jupyter_server_nbmodel/handlers.py", line 275, in _kernel_worker
        results[uid] = await _execute_snippet(
                       ^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/jupyter/notebook-env/lib/python3.11/site-packages/jupyter_server_nbmodel/handlers.py", line 223, in _execute_snippet
        ycell = await _get_ycell(ydoc, metadata)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/jupyter/notebook-env/lib/python3.11/site-packages/jupyter_server_nbmodel/handlers.py", line 101, in _get_ycell
        notebook: YNotebook | None = await ydoc.get_document(room_id=document_id, copy=False)
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/jupyter/notebook-env/lib/python3.11/site-packages/jupyter_server_ydoc/app.py", line 170, in get_document
        room = await self.ywebsocket_server.get_room(room_id)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/jupyter/notebook-env/lib/python3.11/site-packages/jupyter_server_ydoc/websocketserver.py", line 132, in get_room
        await self.start_room(room)
      File "/home/jupyter/notebook-env/lib/python3.11/site-packages/pycrdt_websocket/websocket_server.py", line 96, in start_room
        raise RuntimeError(
    RuntimeError: The WebsocketServer is not running: use `async with websocket_server:` or `await websocket_server.start()`

Expected behavior

Error is propagated to the API

krassowski commented 2 months ago

A simple fix would to to set results[uid] to an a dict containing an "error" key in the except group of:

https://github.com/datalayer/jupyter-server-nbmodel/blob/0a6c6830cfd08dd370d0aeffa8052aae005cd734/jupyter_server_nbmodel/handlers.py#L275-L289

As this would then correctly set 500 status:

https://github.com/datalayer/jupyter-server-nbmodel/blob/0a6c6830cfd08dd370d0aeffa8052aae005cd734/jupyter_server_nbmodel/handlers.py#L596-L602