Skip to content

Decide SIGCHLD at the child's exit - #538

Open
xalestar wants to merge 1 commit into
sysprog21:mainfrom
xalestar:sigchld-at-exit
Open

xalestar wants to merge 1 commit into
sysprog21:mainfrom
xalestar:sigchld-at-exit

Conversation

@xalestar

@xalestar xalestar commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

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 Drop SIGCHLD that trails a reaped child #505, plus WNOWAIT, blocked, SIG_IGN while blocked, A SIGCHLD handler installed after a child exits can run for it where Linux discards the signal #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):

    call upstream/main c57282f this PR
    SIG_DFL over SIG_DFL, SIGCHLD unblocked 0.79 us 18.5 to 18.9 us
    SIG_DFL over SIG_DFL, SIGCHLD blocked 0.77 to 0.78 us 18.8 to 18.9 us
    a handler to SIG_DFL, SIGCHLD unblocked 0.78 to 0.80 us 46.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

cubic-dev-ai[bot]

This comment was marked as resolved.

cubic-dev-ai[bot]

This comment was marked as resolved.

@xalestar
xalestar force-pushed the sigchld-at-exit branch 4 times, most recently from b64c226 to b9adeab Compare November 18, 2026 02:04
Comment thread src/syscall/proc.c Resolved
Comment thread src/syscall/proc.c Resolved
Comment thread src/syscall/proc.c Resolved
Comment thread src/syscall/signal.c Resolved
cubic-dev-ai[bot]

This comment was marked as resolved.

@xalestar
xalestar force-pushed the sigchld-at-exit branch 3 times, most recently from 24f6c25 to cbaf6a8 Compare November 18, 2026 08:20
@xalestar
xalestar requested a review from jserv November 18, 2026 08:40
Comment thread src/syscall/signal.c Resolved
cubic-dev-ai[bot]

This comment was marked as resolved.

Comment thread src/syscall/signal.c Resolved
cubic-dev-ai[bot]

This comment was marked as resolved.

@xalestar
xalestar force-pushed the sigchld-at-exit branch 2 times, most recently from bb5b2f1 to 3772e83 Compare November 18, 2026 16:34
cubic-dev-ai[bot]

This comment was marked as resolved.

cubic-dev-ai[bot]

This comment was marked as resolved.

@xalestar
xalestar force-pushed the sigchld-at-exit branch 2 times, most recently from 51fd40c to e7d21a1 Compare November 19, 2026 09:33
@github-actions

Copy link
Copy Markdown

Benchmark changes outside ±20%

Compared elfuse-aarch64 with the default branch's baseline. Lower is better.

Improvements

Metric Baseline Current Change
applications.git_status_ms 113.327 27.883 -75.4%
applications.make_ms 5850.39 1461.41 -75.0%
applications.python_startup_ms 117.826 25.058 -78.7%
applications.zstd_ms 112.975 33.011 -70.8%
lmbench.lat_proc.exec 112392 23028.4 -79.5%
lmbench.lat_proc.fork 116097 5737.67 -95.1%
lmbench.lat_syscall.read 1.72295 0.9247 -46.3%
lmbench.lat_syscall.stat 2.5146 2.00495 -20.3%
lmbench.lat_syscall.write 1.7114 1.01865 -40.5%

Workflow run · commit e7d21a1ffd47

Comment thread src/syscall/signal.c Resolved
Comment thread src/syscall/proc.c Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 9 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread src/syscall/signal.c Resolved
Comment thread tests/test-sigchld-after-wait.c Resolved
Comment thread tests/test-signal.c Resolved
Linux discards a SIGCHLD at the child's exit when the parent ignores it
there, under SIG_IGN or under SIG_DFL with SIGCHLD unblocked, so a
handler that the parent installs later never runs for that child; a
SIGCHLD that Linux keeps is pending before a wait returns the child
(test-sigchld-after-wait checks both on Linux 6.18). Here the parent
decided when it drained the child's record, and that record could arrive
after the wait. A handler installed after the exit, or after SIGCHLD was
blocked, then ran for the child, and a handler could run after the wait
had returned the child.

Decide at the exit instead, from the parent's disposition and blocked
masks at that moment, and make a kept signal pending before the wait
returns, so lmbench's lat_fs no longer sees a stale SIGCHLD between its
phases and a handler installed after an exit whose SIGCHLD the parent
ignored there does not run.

When SIG_IGN or SIG_DFL replaces the SIGCHLD action, rt_sigaction drains
the parent's records first and drops a pending SIGCHLD from every
pending set, as do_sigaction does; a POSIX timer whose SIGCHLD it drops
fires again. When the call turns the flag file to ignored, a child that
read the flag before the turn may append its record after that drain,
so the call then waits on the lifecycle registry lock, which the child
holds from its flag read to its 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. The ign, dfl,
unblock, exiting, thread-directed and timer cases of
test-sigchld-after-wait and case 7 of test-signal check these on Linux
6.18. A failed open, ftruncate or mmap of the flag file is logged once.

The decision still differs from Linux in five ways (Linux side read in
kernel/signal.c, not run): it counts every thread's mask rather than one
thread's, an exited thread's mask counts until its slot is released, it
does not look at ptrace, a kept signal is delivered under the
disposition in force when the parent drains the record, and a SIG_IGN or
SIG_DFL drops a record still undrained even when SIGCHLD was unblocked
at the exit, where Linux has already run the handler.

ELFUSE_SIGNAL_DRAIN_DELAY_US holds each drain that a SIGUSR2 starts, so
the late cases of test-sigchld-after-wait can change the parent's
SIGCHLD action before the parent drains the child's record, and
ELFUSE_SIGCHLD_DECIDE_DELAY_US holds an exiting child between its flag
read and its append, so the exiting case can install SIG_IGN in
between; without them, a build that lacks one of the parts they cover
passes them, except a build that never drops the pending SIGCHLD.

Closes sysprog21#498
Closes sysprog21#536
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A SIGCHLD handler installed after a child exits can run for it where Linux discards the signal lat_fs times out under elfuse in the CI Benchmark leg

2 participants