Skip to content

Commit

Permalink
Fix detection of parent process death (#7914)
Browse files Browse the repository at this point in the history
This code assumes os.getppid() == 1 is a reliable signal of parent
death, because traditionally, pid=1 adopts orphaned processes.

However, systemd --user instances on linux are processes with pid > 1
that also seem to adopt orphan processes. In such cases, we would miss
parent death, and leak orphaned tail processes.

This changes the detection criteria to "parent pid has changed".
  • Loading branch information
aroig committed May 18, 2022
1 parent 943ab6c commit 448ca76
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def watch(args):

while True:
# check if this process has been orphaned, in which case kill the tail_pid
if os.getppid() == 1:

# we assume that the process is orphaned if parent pid changes. because systemd
# user instances also adopt processes, os.getppid() == 1 is no longer a reliable signal
# for being orphaned.
if os.getppid() != parent_pid:
try:
os.kill(tail_pid, signal.SIGTERM)
except OSError:
Expand Down

0 comments on commit 448ca76

Please sign in to comment.