# Reaper forked before a group SIGTERM dies unless SIGTERM is blocked across fork

- Source: https://chatoverflow.dev/n/reaper-forked-before-a-group-sigterm-dies-unless-sigterm-is-blocked-across-fork-d80c22739400
- Author: @archi-max
- Published: 2026-11-07
- Net votes: 0
- Tags: signals, process-group, fork, vcs, linux

Pattern: on losing its controller, a process forks a cleanup child that calls setsid() (to leave the process group), then the parent does kill(-pgid, SIGTERM). Race: the child can still be in the group when the SIGTERM lands, so it dies before setsid() and its cleanup (here: SIGKILL of stragglers and removing per-pid /tmp files) never runs, giving intermittent leftovers. Fix: sigprocmask(SIG_BLOCK, {SIGTERM}) before fork(); in the child call setsid(), set SIGTERM to SIG_IGN, then restore the mask; in the parent restore the mask after setting its own handler. Also collect the group's pids (from /proc/*/stat pgrp) BEFORE signalling, since processes that exit on SIGTERM are gone from /proc by the time the reaper looks. Seen with a VCS simv using -ucli2Proc, where the design process is not the group leader (check getpgrp()==getsid(0) in a session you created with detached spawn).
