Decide SIGCHLD at the child's exit

XA
1h ago
+1261-85

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, or SIG_DFL with no thread blocking it). It is rewritten when that value changes: SIGCHLD's action, a thread's SIGCHLD mask bit, a thread exit or a CLONE_VM child'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.
  • wait4 and waitid drain the parent's records before returning a child, so a kept signal is pending before the wait returns.
  • Installing SIG_IGN or SIG_DFL for SIGCHLD drains the parent's records first and drops a pending SIGCHLD from the shared and every thread's pending set, as do_sigaction does; 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_sigaction reads the new action before it writes the old one, as Linux does, because the drain needs it first. A failed open, ftruncate or mmap of 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_IGN while blocked, #536; a SIGCHLD sent to the thread with tgkill while blocked, then SIG_IGN or SIG_DFL, the handler again and an unblock; and a periodic POSIX timer that signals SIGCHLD, whose pending signal SIG_IGN drops while blocked, firing again after the unblock). Passes on elfuse and on Linux 6.18 (QEMU). The two tgkill cases 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-signal case 7: rt_sigaction with 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 (in make check): 8 cases under a 250 ms drain delay: a plain late handler, and after an SA_RESETHAND run, a blocking thread's exit, a blocking CLONE_VM child's reap, and an adopted exited grandchild; then ign and dfl, where the child exits under a handler with SIGCHLD blocked and the parent installs SIG_IGN or SIG_DFL, the handler again, and unblocks; and unblock, where the child exits under SIG_DFL with SIGCHLD blocked and the parent unblocks, installs SIG_DFL again, then the handler. Removing one part fails its case: the SA_RESETHAND republish, the thread-exit republish, the CLONE_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 in rt_sigaction, ign, dfl and unblock fail 10 of 10; a build that skips that drain while the flag file says discarded fails unblock 10 of 10. Without the delay, each of those builds passes its cases. A build without the drop fails ign and dfl 10 of 10 with or without the delay. tests/test-matrix.sh runs each case on elfuse and on Linux 6.18; both pass.

  • test-sigchld-exit-race (in make 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 installs SIG_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.sh runs 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):

    callupstream/main c57282fthis PR
    SIG_DFL over SIG_DFL, SIGCHLD unblocked0.79 us18.5 to 18.9 us
    SIG_DFL over SIG_DFL, SIGCHLD blocked0.77 to 0.78 us18.8 to 18.9 us
    a handler to SIG_DFL, SIGCHLD unblocked0.78 to 0.80 us46.8 to 47.0 us

    The difference is the drain, which every SIG_IGN or SIG_DFL for 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 check passes except the BusyBox nslookup lane (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 Rosetta audit-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_join returns; 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_IGN or SIG_DFL drains 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_IGN or SIG_DFL installed 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.

Closes #498
Closes #536