|
| 1 | +# Replace PUB socket with XPUB socket |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +A Jupyter kernel uses a PUB socket as a broadcast channel where it publishes various messages (incoming requests, outputs, events). Any client that wants to be notified of what happens in the kernel can simply subscribe to this channel via a SUB socket. The issue with this simple PUB - SUB pattern is that the kernel has no mean to detect client subscriptions, and it will broadcast messages whether a client has subscribed or not. |
| 6 | + |
| 7 | +This is particularly problematic when a client needs to send a bunch of execution requests just after starting a kernel (a typical use case is the "Restart Kernel and execute all" command of Jupyter Lab, or opening a Notebook with Voilà). In that case, the client needs to ensure that its SUB socket is connected to the PUB socket of the kernel before sending any execution request, otherwise it may miss some outputs. The kernel, on its side, will not emit any event until it receives a request. |
| 8 | + |
| 9 | +## Current solution |
| 10 | + |
| 11 | +The current solution consists of sending `kernel_info` requests until a `kernel_info` reply is received on the SHELL and an idle status message is received on the SUB socket. If the client receives a `kernel_info` reply but not the idle status message before a timeout, this means that the SUB socket has not connected yet. The client discards the reply and send a new `kernel_info` request. |
| 12 | + |
| 13 | +This solution makes the implementation of a Jupyter client more complicated than required. Indeed, ZeroMQ provides a publisher socket that is able to detect new subscriptions. |
| 14 | + |
| 15 | +## Proposed Enhancement |
| 16 | + |
| 17 | +We propose to replace the PUB socket of the kernel with an XPUB socket. When it detects a new subscriber, the kernel sends a welcome message to the client. This way, the connecting sequence of the client is simplified: it only has to wait for a `kernel_info` reply and a welcome message on the SUB socket. |
| 18 | + |
| 19 | +The welcome message should have empty parent header, metadata and content. |
| 20 | + |
| 21 | +### Impact on existing implementations |
| 22 | + |
| 23 | +Although this enhancement requires changing all the existing kernels, the impact should be limited. Indeed, most of the kernels are based on the kernel wrapper approach, or on xeus. |
| 24 | + |
| 25 | +Most of the clients are based on the Jupyter Server. Therefore, |
| 26 | +the changes should be limited to this repository and to the notebook. |
| 27 | + |
| 28 | +A transition period where clients support both mechanisms should allow kernels to gradually migrate to the new version of the protocol. |
| 29 | + |
| 30 | +## Relevant Resources (GitHub repositories, Issues, PRs) |
| 31 | + |
| 32 | +### GitHub repositories |
| 33 | + |
| 34 | +- Jupyter Client: https://github.com/jupyter/jupyter_client |
| 35 | +The Jupyter protocol client APIs |
| 36 | +- Jupyter Server: https://github.com/jupyter-server/jupyter_server |
| 37 | +The backend to Jupyter web applications |
| 38 | +- Jupyter Notebook: https://github.com/jupyter/notebook |
| 39 | +The Jupyter interactive notebook |
| 40 | +- IPyKernel: https://github.com/ipython/ipykernel |
| 41 | +IPython kernel for Jupyter |
| 42 | +- Xeus: https://github.com/jupyter-xeus/xeus |
| 43 | +The C++ implementation of the Jupyter kernel protocol |
| 44 | + |
| 45 | +### GitHub Issues |
| 46 | + |
| 47 | +- Main issue in jpuyter_client: SUB sockets take time to subscribe to the IOPub channel and miss important messages (https://github.com/jupyter/jupyter_client/issues/593) |
| 48 | +- Related issue in Xeus for implementation detail: Implement Last Value Cache pattern for iopub (https://github.com/jupyter-xeus/xeus/issues/266) |
| 49 | + |
| 50 | +### GitHub Pull Requests |
| 51 | + |
| 52 | +- in jupyter_client: retry kernel_info_requests in wait_for_ready (https://github.com/jupyter/jupyter_client/pull/592) |
| 53 | +- in jupyter_server: Nudge kernel with info request until we receive IOPub messages (https://github.com/jupyter-server/jupyter_server/pull/361) |
| 54 | +- in notebook: ensure iopub subscriptions propagate prior to accepting websocket connections (https://github.com/jupyter/notebook/pull/5908) |
0 commit comments