jupyter / jupyter_console

Jupyter Terminal Console

Home Page:http://jupyter-console.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Kernel runs in background when frontend is killed

xuesoso opened this issue · comments

commented

Hi, I noticed that when I directly close the frontend program that called "jupyter-console" (e.g. my terminal), it leaves the "python -m ipykernel_launcher -f ~/.local/share/jupyter/runtime/kernel-PID.json" running in the background. I would like to kill the kernel session created by "jupyter-console" whenever its host frontend program is killed / closed. Is there any option or configuration where I can achieve that? Thank you!

commented

I think I have some clues as to the reason why. First to provide some of the relevant observations:

  1. On a remote machine without interactive login session, killing the parent frontend (e.g. a Tmux session or terminal) hosting the Jupyter console will lead to the expected killing of ipykernel on the background.
  2. On another machine running Manjaro Gnome, killing the parent frontend (e.g. a Tmux session or terminal) will not kill the ipykernel subprocess. I can see that it's still dangling in the background.

Chasing down the rabbit hole, I found additional observations:

  1. "ipykernel/parentpoller.py" : is responsible for continuous monitoring of parent process that spawned ipykernel. "Line 36" makes the assumption that if the parent of orphaned process is killed, its ID gets set to "1", in which case the kernel will exit itself.
    parentpoller_relevant_code
  2. After adding in a line to log the parent process ID, I found that on the Manjaro Gnome machine, the initial parent process ID transfers from the "jupyer-console":
    initial_ppid
    after killing the initial parent process to:
    post_parent_killed_ppid

ppid

A few additional posts suggest that the parent ID of orphaned processes may not be necessarily set to 1 if there are ancestor processes. I will also raise this issue separately on the "ipykernel".