Description
ParentPollerUnix never shuts the kernel down if the parent process exits while the kernel is still starting up and orphans are reparented to a subreaper (systemd --user, WSL's /init, a container init that sets PR_SET_CHILD_SUBREAPER, …). The kernel keeps running forever.
Why
ParentPollerUnix.run() takes its snapshot of the parent only when the thread starts, which is in IPKernelApp.start(), after initialize() has loaded extensions and run exec_files:
original_ppid = os.getppid()
if original_ppid != self.parent_pid:
self.parent_pid = 0If the parent is already gone by then, getppid() returns the subreaper, parent_pid is reset to 0, and the loop only exits on ppid == 1, which never happens under a subreaper. #1271 mentions this as a side effect ("if the parent dies quickly enough, ipykernel does not notice"); the consequence is a kernel that outlives its parent indefinitely, and the window is the whole startup, including however long the startup files take.
Reproduce
The script makes itself a subreaper, lets a "parent" launch a kernel the way jupyter_client does (JPY_PARENT_PID = its own pid) with a startup file that takes 2 s, and exits the parent either during startup or after it.
upstream_poller.py
"""ipykernel keeps running forever when its parent exits during startup and orphans are reparented to a subreaper.
Run: python upstream_poller.py
"""
import ctypes, os, subprocess, sys, tempfile, time
from pathlib import Path
PR_SET_CHILD_SUBREAPER = 36
ctypes.CDLL(None, use_errno=True).prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0) # like systemd --user or WSL's init
def trial(parent_lives_for: float) -> None:
work = Path(tempfile.mkdtemp())
(work / "slow_startup.py").write_text("import time; time.sleep(2)\n") # e.g. a startup file that takes a while
# The "parent" launches the kernel the way jupyter_client does (JPY_PARENT_PID = its own pid), then exits.
parent = subprocess.Popen([sys.executable, "-c", f"""
import os, subprocess, sys, time
kernel = subprocess.Popen([sys.executable, "-m", "ipykernel_launcher", "-f", {str(work / "k.json")!r},
"--IPKernelApp.exec_files={work / "slow_startup.py"}"],
env={{**os.environ, "JPY_PARENT_PID": str(os.getpid())}}, start_new_session=True,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print(kernel.pid, flush=True)
time.sleep({parent_lives_for})
"""], stdout=subprocess.PIPE, text=True)
kernel = int(parent.stdout.readline())
parent.wait()
time.sleep(8)
alive = Path(f"/proc/{kernel}").exists() and "Z" not in Path(f"/proc/{kernel}/stat").read_text().rsplit(")", 1)[1].split()[0]
print(f"parent exited {parent_lives_for:>3} s after launching the kernel; 8 s later the kernel is {'STILL RUNNING' if alive else 'gone'}")
if alive:
os.kill(kernel, 9)
try:
os.waitpid(kernel, 0) # we are its subreaper now
except ChildProcessError:
pass
import ipykernel; print("ipykernel", ipykernel.__version__, "| Python", sys.version.split()[0])
trial(0.2) # parent dies while the kernel is still starting
trial(5.0) # parent dies after the kernel has startedOutput:
ipykernel 7.4.0 | Python 3.13.16
parent exited 0.2 s after launching the kernel; 8 s later the kernel is STILL RUNNING
parent exited 5.0 s after launching the kernel; 8 s later the kernel is gone
Possible direction
When getppid() differs from JPY_PARENT_PID, two cases are folded together: the kernel was started through a wrapper (the case #1271 keeps working by falling back to the old behaviour), or the parent has already exited. They can be told apart by checking whether parent_pid still exists (os.kill(parent_pid, 0), or os.pidfd_open(parent_pid) on Linux): if it does not, the parent is gone and the kernel should exit. Taking the getppid() snapshot in ParentPollerUnix.__init__ (called from init_poller(), before sockets, extensions and exec_files) would also shrink the window considerably.
Versions: ipykernel 7.4.0, Python 3.13.16, Linux (WSL2). ipykernel/parentpoller.py is unchanged on main as of today.
I ran into this while building a tool that supervises ipykernel processes; the investigation and this write-up were done with an AI assistant (🤖🤖). The output above is pasted from an actual run.
Activity