Skip to content

Commit 3615c2d

Browse files
committed
Allow for kernel I/O to be disconnected by a client manually
Previously, the kernel I/O would be established at client creation time and was unable to be disconnected for the lifetime of the client. The issue with this was that the same kernel could be used for different clients at different times, as such a client may receive unwanted kernel messages at those later times when another client is connected. This change allows for a manual disconnect from the kernel's I/O and connecting to it again at a later time. * jupyter-client.el (jupyter-connect): New function. (jupyter-disconnect): Modified to account for change. * jupyter-kernel-process.el (jupyter-zmq-io): Add connect and disconnect messages to allow for a client to start and stop the I/O connection at will. * jupyter-server-kernel.el (jupyter-websocket-io): Ditto. Removed the `shutdown` indicator.
1 parent 698815e commit 3615c2d

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

jupyter-client.el

+12-1
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,19 @@ KERNEL is the name of the kernelspec as returned by the
411411
shell command."
412412
(jupyter-client (jupyter-get-kernelspec kernel) client-class))
413413

414+
(cl-defmethod jupyter-connect ((client jupyter-kernel-client))
415+
"Connect to the I/O of a CLIENT's kernel.
416+
If the I/O of the CLIENT to the kernel is disconnected, it means
417+
messages will no longer be received on the client side although
418+
messages from other client's could still be exchanged between the
419+
kernel and those others."
420+
(jupyter-run-with-io (jupyter-kernel-action-subscriber client)
421+
(jupyter-publish (list 'connect))))
422+
414423
(cl-defmethod jupyter-disconnect ((client jupyter-kernel-client))
415-
(slot-makeunbound client 'io))
424+
"Disconnect from the I/O of a CLIENT's kernel."
425+
(jupyter-run-with-io (jupyter-kernel-action-subscriber client)
426+
(jupyter-publish (list 'disconnect))))
416427

417428
(cl-defmethod jupyter-client ((spec jupyter-kernelspec) &optional client-class)
418429
"Return a client connected to kernel created from SPEC.

jupyter-kernel-process.el

+3
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ Call the next method if ARGS does not contain a :spec or
252252
(jupyter-shutdown kernel)
253253
(stop)
254254
(setq shutdown t))
255+
((and op (or 'connect 'disconnect))
256+
(if (eq op 'connect) (stop)
257+
(start)))
255258
('restart
256259
(setq shutdown nil)
257260
(jupyter-restart kernel)

jupyter-server-kernel.el

+10-8
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,12 @@ this case FN will be evaluated on KERNEL."
240240
(reauth-pub (or (gethash server jupyter--reauth-subscribers)
241241
(setf (gethash server jupyter--reauth-subscribers)
242242
(jupyter-publisher))))
243-
(shutdown nil)
244243
(kernel-io
245244
(jupyter-publisher
246245
(lambda (event)
247246
(pcase event
248247
(`(message . ,rest) (jupyter-content rest))
249248
(`(send ,channel ,msg-type ,content ,msg-id)
250-
(when shutdown
251-
(error "Attempting to send message to shutdown kernel"))
252249
(let ((send
253250
(lambda ()
254251
(websocket-send-text
@@ -264,8 +261,6 @@ this case FN will be evaluated on KERNEL."
264261
(setq ws (funcall make-websocket))
265262
(funcall send)))))
266263
('start
267-
(when shutdown
268-
(error "Can't start I/O connection to shutdown kernel"))
269264
(unless (websocket-openp ws)
270265
(setq ws (funcall make-websocket))))
271266
('stop (websocket-close ws))))))
@@ -313,11 +308,18 @@ this case FN will be evaluated on KERNEL."
313308
(pcase action
314309
('interrupt
315310
(jupyter-interrupt kernel))
311+
((and op (or 'connect 'disconnect))
312+
(if (eq op 'disconnect)
313+
(progn
314+
(or (null ws)
315+
(websocket-close ws))
316+
(setq ws (funcall make-websocket)))
317+
(websocket-close ws)
318+
(setq ws nil)))
316319
('shutdown
317320
(jupyter-shutdown kernel)
318-
(setq shutdown t)
319-
(when (websocket-openp ws)
320-
(websocket-close ws)))
321+
(websocket-close ws)
322+
(setq ws nil))
321323
('restart
322324
(jupyter-restart kernel))
323325
(`(action ,fn)

0 commit comments

Comments
 (0)