Decide SIGCHLD at the child's exit
Intent: Decide at the child's exit whether its SIGCHLD is discarded, as Linux does, instead of when the parent drains the child's record.
Failure fixed: The record can reach the parent after the wait that reaped the child, so a handler installed in between ran for that child. lmbench's lat_fs hung on this until the CI timeout (#498; lat_fs -N 1 -s 1024 in four loops beside ten busy processes timed out in 3 of 96 runs on upstream/main and 0 of 96 here), and a handler installed after an unwaited exit, or after blocking SIGCHLD, also ran (#536).
This PR:
- Each process keeps a small mapped flag file: "a SIGCHLD generated now would be discarded" (
SIG_IGN, orSIG_DFLwith no thread blocking it). It is rewritten when that value changes: SIGCHLD's action, a thread's SIGCHLD mask bit, a thread exit or aCLONE_VMchild's reap. The first republish writes the file, and every fork republishes before the child can run, so the file exists before any child can exit. - The exiting child reads its parent's flag file under the lifecycle registry lock and marks its record when the signal is discarded; an exited child reparented at that exit is marked from the adopter's flag file. The parent does not queue a marked record's signal.
wait4andwaitiddrain the parent's records before returning a child, so a kept signal is pending before the wait returns.- Installing
SIG_IGNorSIG_DFLfor SIGCHLD drains the parent's records first and drops a pending SIGCHLD from the shared and every thread's pending set, asdo_sigactiondoes; a POSIX timer whose SIGCHLD it drops fires again. When the call turns the flag file to discarded, a child that read the flag before the turn may append its record after that drain, so the call then takes the lifecycle registry lock shared, which waits for such a child's append, drains again and drops that SIGCHLD too.rt_sigactionreads the new action before it writes the old one, as Linux does, because the drain needs it first. A failedopen,ftruncateormmapof the flag file is logged once. ELFUSE_SIGNAL_DRAIN_DELAY_US, a test-only knob, holds the parent's drain after the doorbell (SIGUSR2), so a test can change the SIGCHLD action before the drain.ELFUSE_SIGCHLD_DECIDE_DELAY_US, another, holds an exiting child between its flag file read and its append.
This replaces #505 (1036 lines added in 18 files), which decided at read time.
Verification:
-
test-sigchld-after-wait: 13 cases (6 from #505, plus WNOWAIT, blocked,SIG_IGNwhile blocked, #536; a SIGCHLD sent to the thread withtgkillwhile blocked, thenSIG_IGNorSIG_DFL, the handler again and an unblock; and a periodic POSIX timer that signals SIGCHLD, whose pending signalSIG_IGNdrops while blocked, firing again after the unblock). Passes on elfuse and on Linux 6.18 (QEMU). The twotgkillcases fail 10 of 10 rounds on a build that never drops a pending SIGCHLD and on one that drops it only from the shared set; the timer case fails 10 of 10 on a build that drops the signal without settling its timer. -
test-signalcase 7:rt_sigactionwith act and oldact in one buffer installs act, and a faulting act leaves every byte of oldact unwritten. Passes on elfuse and on Linux 6.18; fails on a build that writes oldact first. -
test-sigchld-late-drain(inmake check): 8 cases under a 250 ms drain delay: a plain late handler, and after anSA_RESETHANDrun, a blocking thread's exit, a blockingCLONE_VMchild's reap, and an adopted exited grandchild; thenignanddfl, where the child exits under a handler with SIGCHLD blocked and the parent installsSIG_IGNorSIG_DFL, the handler again, and unblocks; andunblock, where the child exits underSIG_DFLwith SIGCHLD blocked and the parent unblocks, installsSIG_DFLagain, then the handler. Removing one part fails its case: theSA_RESETHANDrepublish, the thread-exit republish, theCLONE_VM-reap republish and the adopter's flag read each fail their case in 9 or 10 of 10 rounds; never marking the record fails the first 5 (8 to 10 of 10); without the drain inrt_sigaction,ign,dflandunblockfail 10 of 10; a build that skips that drain while the flag file says discarded failsunblock10 of 10. Without the delay, each of those builds passes its cases. A build without the drop failsignanddfl10 of 10 with or without the delay.tests/test-matrix.shruns each case on elfuse and on Linux 6.18; both pass. -
test-sigchld-exit-race(inmake check):exiting, under a 300 ms decide delay: the child exits under a handler with SIGCHLD blocked, and 100 ms after its last write the parent installsSIG_IGN, the handler again, and unblocks. It fails 10 of 10 rounds on a build without the second drain and on one that drains again without taking the registry lock, and passes without the delay on both.tests/test-matrix.shruns it on elfuse and on Linux 6.18; both pass. -
sigaction(SIGCHLD, ...)per call after one child has exited, mean of 20000 calls, three runs of each build, interleaved. The last row times each call alone and subtracts the cost of an empty timed sample, since each sample includes part of a clock read (0.79 us under elfuse):call upstream/main c57282f this PR SIG_DFLoverSIG_DFL, SIGCHLD unblocked0.79 us 18.5 to 18.9 us SIG_DFLoverSIG_DFL, SIGCHLD blocked0.77 to 0.78 us 18.8 to 18.9 us a handler to SIG_DFL, SIGCHLD unblocked0.78 to 0.80 us 46.8 to 47.0 us The difference is the drain, which every
SIG_IGNorSIG_DFLfor SIGCHLD runs. The last row turns the flag file to discarded on every call, so it also waits on the registry lock and drains a second time. -
make checkpasses except the BusyBoxnslookuplane (timeout after 10 s), which fails the same way on an upstream/main build.make test-matrix: elfuse-aarch64 328 passed, qemu-aarch64 302 passed, no failures except the Rosettaaudit-known-limitations, which fails on upstream/main too.
Differences from Linux (read in kernel/signal.c and kernel/exit.c, not run):
- The republish reads every thread's mask; Linux reads only the thread the signal targets. A SIGCHLD that only another thread blocks is kept here.
- After a thread exits, its mask still counts until its slot is released, which can come after
pthread_joinreturns; a child that exits in that window keeps its SIGCHLD. - ptrace is ignored; Linux keeps SIGCHLD for a traced parent under
SIG_DFL(sig_ignored). - A kept SIGCHLD is delivered under the disposition in force when the parent drains the record, not at the exit. A
SIG_IGNorSIG_DFLdrains and drops it first. A child that cannot open the lifecycle registry sends its record without the registry lock, and that record can still arrive after the call. - With SIGCHLD unblocked at the exit, a
SIG_IGNorSIG_DFLinstalled before the drain drops the record, where Linux has already run the handler.
Not tested: a traced parent; a failed ftruncate or mmap of the flag file (the parent then decides as before, after one warning; a failed open was run once by hand, with a directory at the file's path, and logged one line); the release of a waiter's claim on a SIGCHLD that the drop removes.
The stale-ppid race in reparent records predates this PR and is tracked in #539. Also older than this PR: when a handler replaces SIG_IGN, elfuse reaps a child that exited under the earlier handler, and Linux keeps that zombie; the ign case accepts either.