Skip to content

Commit 77a40e8

Browse files
committed
Incorporated feedbacks
1 parent 7cfff9b commit 77a40e8

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

jupyter-xpub/jupyter-xpub.md

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Problem
44

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.
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 **there is no simple mechanism for clients to wait for their iopub subscription to be established.**
66

77
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.
88

@@ -14,18 +14,52 @@ This solution makes the implementation of a Jupyter client more complicated than
1414

1515
## Proposed Enhancement
1616

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.
17+
We propose to replace the PUB socket of the kernel with an XPUB socket. XPUB sockets receive the following events:
18+
- subscribe (single frame messages with `\1{subscription-topic}`)
19+
- unsubscribe (single frame messages with `\0{subscription-topic}`)
20+
21+
When the IOPub XPUB socket receives an event indicating a new subscription, it shall send an `iopub_welcome` message with no parent header and the received
22+
subscription in the `subscription` field, *with the given subscription topic*:
23+
24+
```
25+
identity_prefix: ["subscription-topic"]
26+
parent_header: {}
27+
header: {
28+
"msg_type": "iopub_welcome"
29+
}
30+
content: {
31+
"subscription": "subscription-topic"
32+
}
33+
metadata: {}
34+
```
35+
36+
Notes:
37+
38+
- The identity prefix may contain extra information about the message, such as the kernel id, according to the convention used in the IPython kernel:
39+
```kernel.{u-u-i-d}.subscription-topic```.
40+
- Subscriptions can be empty (this is almost always the case). An empty subscription means subscribing to all IOPub messages.
41+
- Subscriptions shall be UTF-8 encoded. Non-utf8 subscriptions shall be ignored, and not produce a welcome message.
42+
- Every subscribed client will receive every other client's welcome message, assuming they match existing subscriptions (e.g. empty strings)
43+
- Welcome messages do not and cannot identify the client whose subscription is being received. Receipt of an iopub_welcome message with your subscription does not mean it is in response to your own subscription. However, receiving a message does mean that a matching subscription has been registered for your client, otherwise no message will be received. So if only one subscription is registered, as is normally the case, receiving any welcome message is sufficient to indicate that your client's subscription is fully established. The gist is that receiving a welcome message is a sufficient condition to establish the subscription-propagation event, and additional welcome messages should be expected and ignored.
44+
- Unsubscribe events are ignored.
2045

2146
### Impact on existing implementations
2247

2348
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.
2449

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.
50+
Regarding clients, this change is backward-incompatible only when they start assuming a welcome message will come. Clients not aware of
51+
the new message will just get an unrecognized iopub message they can safely ignore.
52+
53+
Most of the clients are based on the Jupyter Server. Therefore, the changes should be limited to this repository and to the notebook. The new
54+
recommended starting procedure is:
55+
56+
1. always probe with a single `kernel_info_request` (required for protocol adaptation)
57+
2. check protocol version in reply
58+
i. if welcome message is supported, wait for it on iopub
59+
ii. if not:
60+
a. use current request & wait loop (recommended, especially for current clients that already have this implemented)
61+
b. accept and warn about possible loss of early iopub messages for 'old' kernels ((increasingly common over time as protocol updates can be more safely assumed, especially for new clients
2762

28-
A transition period where clients support both mechanisms should allow kernels to gradually migrate to the new version of the protocol.
2963

3064
## Relevant Resources (GitHub repositories, Issues, PRs)
3165

@@ -44,7 +78,7 @@ The C++ implementation of the Jupyter kernel protocol
4478

4579
### GitHub Issues
4680

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)
81+
- Main issue in jupyter_client: SUB sockets take time to subscribe to the IOPub channel and miss important messages (https://github.com/jupyter/jupyter_client/issues/593)
4882
- Related issue in Xeus for implementation detail: Implement Last Value Cache pattern for iopub (https://github.com/jupyter-xeus/xeus/issues/266)
4983

5084
### GitHub Pull Requests

0 commit comments

Comments
 (0)