From eb92b10bd23e4f2cedf27c6ae13462e1b3e5cef0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ludovic=20Court=C3=A8s?= <ludovic.courtes@inria.fr>
Date: Sun, 24 Jan 2021 16:07:40 +0100
Subject: [PATCH] kernel: When switching inferiors, terminate the previous one
 forcefully.

This fixes a bug whereby ";;guix pin" would sometimes hang in the
'close-inferior' call in 'set-proxy-state-inferior'.

* guix-jupyter-kernel.scm (set-proxy-state-inferior):
Send (primitive-exit 0) to PREVIOUS.
---
 guix-jupyter-kernel.scm | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/guix-jupyter-kernel.scm b/guix-jupyter-kernel.scm
index e8df846..2200b81 100644
--- a/guix-jupyter-kernel.scm
+++ b/guix-jupyter-kernel.scm
@@ -91,7 +91,16 @@
 was already an inferior associated with STATE, close it."
   (let ((previous (proxy-state-inferior state)))
     (when previous
+      ;; XXX: 'close-inferior' sometimes isn't enough and it ends up being
+      ;; blocked in waitpid(2) (from 'close-pipe') while the inferior is
+      ;; stuck in read(2).  Thus, forcefully terminate PREVIOUS beforehand.
+      ;; This leads to an 'inferior-eval' exception because the inferior
+      ;; doesn't respond, which we catch.
+      (false-if-exception
+       (inferior-eval '(primitive-exit 0) previous))
+
       (close-inferior previous))
+
     (set-proxy-state-property state %inferior-property
                               inferior)))
 
-- 
GitLab