Re: Kill sends signal to wrong process group with job control enabled
Ben Ashton <[email protected]>
| Newsgroups | gmane.comp.shells.bash.bugs |
|---|---|
| Message-ID | <[email protected]> |
On 01/03/2026 01:38, Martin D Kealey wrote: > > On Sat, 28 Feb 2026, 17:30 Ben Ashton, <[email protected]> wrote: > > … > We now have sleep running as a child of the subshell as a process > group leader. > > > However in certain, somewhat contrived, circumstances, bash's > builtin kill > will send the signal to the WRONG process group. > > > Sending SIGTERM to process group: 5710 (kill -TERM -5710) > [pid 5709] kill(-5709, SIGTERM) = 0 > > As you can see we ran `kill -TERM -5710`, but bash actually sent > it to process group 5709 (the subshell itself). If you don't use the > builtin kill then it works as expected > … > Here is the script to reproduce the issue: > > > > setsid sleep 10 & > pid=$! > echo "> Started new process group $pid" > > … > > echo "> Sending SIGTERM to process group: $pid (kill -TERM > -$pid)" > kill -TERM -$pid > > > This is the core error: $! records a PID, NOT a PGID or a SID. On some > systems (e.g. Linux) a PGID or SID is always numerically equal to the > PID of the leader process, on others (e.g. BSD) they are unequal. > > So what would you expect to happen if you go «kill -INT -$pid» on a > systems where pgrps and their respective leader pids are not identical? > Would you want it to translate the PID to its pgrps, or would you > prefer it to fail? > > If it's going to do translations on BSD, why should it NOT do so on > Linux? > > -Martin I think you misunderstand the issue. It doesn't matter how I obtain the PGID. The issue is that I'm running "kill -TERM -SOME_PGID" and the syscall bash is making is "kill(-DIFFERENT_PGID, SIGTERM)". That's objectively wrong. I don't expect any translation, I just expect it to kill the process group that I tell it to. It's unrelated, but I'm curious what systems don't make the PGID the same as the PID of the leader? I thought that was a POSIX thing. The FreeBSD syscall manual for "setsid" states: "the setsid() system call returns the value of the process group ID of the new process group, which is the same as the process ID of the calling process." https://man.freebsd.org/cgi/man.cgi?query=setsid&apropos=0&sektion=2&manpath=FreeBSD+16.0-CURRENT&format=html