Skip to content

Decide SIGCHLD at the child's exit - #538

Open
xalestar wants to merge 3 commits into
sysprog21:mainfrom
xalestar:sigchld-at-exit
Open

xalestar wants to merge 3 commits into
sysprog21:mainfrom
xalestar:sigchld-at-exit

Conversation

@xalestar

@xalestar xalestar commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Intent: Decide whether a SIGCHLD is discarded at the point where Linux decides it, at the child's exit or at a kill, instead of when the parent later drains the child's record.

Failure fixed: A child's SIGCHLD 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). A handler installed after an unwaited exit or after blocking SIGCHLD also ran (#536), and so did one installed after a kill under SIG_DFL.

This PR, in three commits:

  1. No behavior change: the wait4 and waitid bodies move into wait4_once and waitid_once, and proc_append_guest_signal is split out of proc_send_guest_signal.
  2. Decide at the exit.
    • Each process keeps a small mapped file, the flag file. Its one word says whether a SIGCHLD from an exit would be discarded now (SIG_IGN, or SIG_DFL with no thread blocking it), whether one from a kill would be (SIG_IGN or SIG_DFL, with no thread blocking it), and how many times rt_sigaction has dropped pending SIGCHLD. The process rewrites the word when SIGCHLD's action or a thread's SIGCHLD mask bit changes, when a thread exits and when a CLONE_VM child is reaped. Every fork writes it before the child can run.
    • The exiting child reads its parent's word (the adopter's, for a child reparented at that exit) and marks its record discarded, or kept with the drop count it read.
    • wait4 and waitid drain the records before they return a child, so a kept SIGCHLD is pending when the wait returns.
    • SIG_IGN or SIG_DFL for SIGCHLD drains first, then drops pending SIGCHLD from every pending set as do_sigaction does, and raises the drop count. The drain discards a kept record with an older count, which covers a child that read the word before the call and appended after it.
    • Drains are serialized, and their reparent and auto-reap work runs after the transport's flock is released, because an exiting child holds the lifecycle registry lock while it appends to the transport.
    • rt_sigaction reads the new action before it writes the old one, as Linux does. A failed open, ftruncate or mmap of the flag file is logged once.
  3. Decide a kill at the sender. A SIGCHLD kill from another process reads the target's word. A kill, pidfd_send_signal or rt_sigqueueinfo that a process sends itself is decided under sig_lock before it is queued.

Three test-only knobs: ELFUSE_SIGNAL_DRAIN_DELAY_US delays each drain, ELFUSE_SIGCHLD_DECIDE_DELAY_US delays an exiting child between its read and its append, and ELFUSE_EXIT_SKIP_REGISTRY=1 makes an exit skip the lifecycle registry.

This replaces #505, which decided when the parent drained the record.

Verification: every case passes on elfuse and on Linux 6.18.63 (QEMU). The last column names each part whose removal, or a broken version, fails the matching cases in 10 of 10 rounds.

test what it checks fails without
test-sigchld-after-wait, 17 cases the 6 from #505; WNOWAIT; blocked; SIG_IGN while blocked; #536; a tgkill SIGCHLD, then SIG_IGN or SIG_DFL; a periodic timer whose SIGCHLD is dropped; SIG_DFL and a kill under SIG_IGN keep a blocked SIGCHLD; a SIGCHLD sent to itself four ways does not wake a signalfd the drop from every pending set; the timer settle; the mask check under SIG_DFL; the drop count; the kill rule; the decision before a self-sent signal is queued
test-signal case 7 act and oldact in one buffer install act; a faulting act leaves oldact unwritten reading act before writing oldact
test-sigchld-late-drain, 13 cases, 250 ms drain delay a handler installed late: plain, after SA_RESETHAND, after a thread exit, after a CLONE_VM reap, for an adopted child; ign, dfl, unblock; kill, killdfl, killchild; waitpid, waitid the rewrite after SA_RESETHAND, a thread exit or a CLONE_VM reap; the adopter's read; the kill read; the drain in rt_sigaction, wait4 and waitid; marking the record; the drop
test-sigchld-exit-race, 3 cases, 300 ms decide delay the action changes between the child's read and its append the drop count at the drain
test-sigchld-exit-fallback an exit without the registry under SIG_IGN with SIGCHLD blocked the exit rule on that path

Cost, as the mean of 20000 sigaction calls after one child has exited and of 300 waitpid rounds, over three interleaved runs of each build:

call upstream/main c57282f this PR
sigaction(SIGCHLD, SIG_DFL) over SIG_DFL, unblocked 0.79 to 0.81 us 3.50 to 3.54 us
the same, SIGCHLD blocked 0.77 to 0.78 us 3.50 to 3.54 us
a handler to SIG_DFL, each call timed alone 0.78 us 3.56 to 3.63 us
waitpid of a child that has already exited 65 to 66 us 69 to 70 us
fork, exit and waitpid 5.4 to 5.5 ms 5.5 to 5.6 ms

Most of the added sigaction cost is the drain's stat of the empty transport: without that drain a call takes 0.84 to 1.23 us.

make check passes (its BusyBox nslookup lane skipped: no reachable nameserver). make test-matrix: 336 passed on elfuse-aarch64 and 310 on qemu-aarch64; the only failure is the Rosetta audit-known-limitations, which fails on upstream/main too.

Differences from Linux (by reading kernel/signal.c and kernel/exit.c, except where measured):

  • The word reads every thread's SIGCHLD mask; Linux reads only the target thread's. So a SIGCHLD that only another thread blocks is kept here. A CLONE_VM child's mask counts until it is reaped, and an exited thread's until its slot is released, which can be after pthread_join returns.
  • ptrace is ignored. For a traced target, Linux keeps a SIGCHLD from an exit under SIG_DFL and from a kill under SIG_IGN or SIG_DFL.
  • A kept SIGCHLD becomes pending at the drain, under the disposition in force then. After an unblock under SIG_DFL, a handler installed before the drain runs (measured with a 300 ms decide delay: 5 of 5 rounds here, 0 of 5 on Linux 6.18). With SIGCHLD unblocked, a SIG_IGN or SIG_DFL installed before the drain drops a record whose handler Linux has already run.
  • A record whose sender could not read the target's flag file is decided at the drain. That includes a fork child that has not changed SIGCHLD's action or mask, which has no flag file yet.
  • A SIGCHLD sent to one thread (tgkill, tkill, rt_tgsigqueueinfo) is still decided at delivery, so under SIG_IGN or SIG_DFL it wakes a signalfd (measured: 6 of 6 combinations here, 0 of 6 on Linux 6.18). It is part of signal_queue keeps an ignored signal pending where Linux drops it #515.

Not tested:

  • a handler that another thread installs between a self-sent SIGCHLD and its delivery
  • a traced parent
  • a failed ftruncate or mmap of the flag file (a failed open was run by hand: one log line, and a SIGCHLD block or unblock still took 0.82 us)
  • the release of a waiter's claim on a dropped SIGCHLD
  • the drains on a wait4 that returns EFAULT and under auto-reap
  • the rewrite at execve, under the temporary masks of ppoll, pselect6 and epoll_pwait, at rt_sigsuspend and at fault delivery
  • the drain's lock changes (by reading, no deadlock: an exiting child takes the transport's flock while it holds the lifecycle registry lock, and the drain no longer takes that registry lock under the flock)

Older than this PR: the stale-ppid race in reparent records (#539), and elfuse reaping a child that exited under an earlier handler when a handler replaces SIG_IGN, where Linux keeps the zombie (#546; the ign case accepts either). A host signal that ends elfuse leaves its flag file behind, as it leaves the registry files (#548).

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.

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
sys_wait4 and sys_waitid become wrappers around wait4_once and
waitid_once, which hold the old bodies and their retries, and
proc_send_guest_signal splits into proc_append_guest_signal, which
writes the record, and the SIGUSR2 to the target. Nothing changes
behavior; the next commit adds a drain after each wait and appends
records without sending SIGUSR2 at once. check-eintr-contract.py follows
the EINTR returns into wait4_once and waitid_once, and the comments that
named sys_wait4 for code now in wait4_once name it.
Comment thread src/syscall/proc.c Resolved
Comment thread src/syscall/signal.c Resolved
Comment thread src/syscall/signal.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 6 files (changes from recent commits).

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

View guided diff | Re-trigger cubic

Comment thread src/syscall/proc.c Resolved
@xalestar
xalestar force-pushed the sigchld-at-exit branch 2 times, most recently from 702159e to c8f9879 Compare November 23, 2026 07:21

@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 8 files (changes from recent commits).

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

View guided diff | Re-trigger cubic

Comment thread docs/testing.md 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.
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. Each process keeps a small mapped flag file
whose word says whether a SIGCHLD that an exit generated now would be
discarded, and rewrites it when its SIGCHLD action, a thread's SIGCHLD
mask bit, a thread exit or a CLONE_VM child's reap changes that. The
exiting child reads its parent's word, under the lifecycle registry lock
or, when it cannot open the registry, without it, and marks its record
discarded or kept. wait4 and waitid drain the records before they return
a child, so a kept signal is pending by then. lmbench's lat_fs no longer
sees a stale SIGCHLD between its phases.

When SIG_IGN or SIG_DFL replaces the SIGCHLD action, rt_sigaction drains
the parent's records first, for a record left undecided, then drops a
pending SIGCHLD from every pending set, as do_sigaction does; a periodic
POSIX timer whose SIGCHLD it drops fires again. The same call raises a
drop count that the word carries. A kept record carries the count its
child read, and the drain discards it when the count has changed since,
so a child that read the word before the call and appends after it loses
its SIGCHLD too. rt_sigaction reads the new action before it writes the
old one, as Linux does. A failed open, ftruncate or mmap of the flag
file is logged once and not retried. The drain returns at once when the
transport is empty, and runs the reparent and auto-reap work a record
asks for after it releases the transport lock, because an exiting child
holds the lifecycle registry lock while it appends there. Every case of
test-sigchld-after-wait, and case 7 of test-signal, passes on Linux 6.18
too.

The decision still differs from Linux (read in kernel/signal.c and
kernel/exit.c unless noted): it counts every thread's mask, a CLONE_VM
child's until the reap and an exited thread's until its slot is
released, where Linux reads the one thread the signal targets; it does
not look at ptrace; a kept SIGCHLD is delivered under the disposition in
force at the drain (a probe measured 5 of 5 rounds here, 0 of 5 on Linux
6.18); a kill from another process, and a record whose sender could not
read the flag file, are decided at the drain; 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.

Closes sysprog21#498
Closes sysprog21#536
sig_ignored (kernel/signal.c) decides a SIGCHLD sent with kill when it
is generated: it is discarded under SIG_IGN or SIG_DFL, and kept while
it is blocked, under SIG_IGN too. Here a kill from another process left
its record for the drain to decide, so a handler that the target
installed before the drain ran for a SIGCHLD that Linux had discarded.
A kill, pidfd_send_signal or rt_sigqueueinfo that a process sent to
itself was queued and decided at delivery, so it woke a signalfd for
SIGCHLD, and a handler that another thread installed in between would
run for it (by reading, not run).

The flag file's word gains a bit for a kill. A kill from another
process reads the target's word, as an exiting child reads its
parent's, and marks its record discarded or kept with the drop count it
read; a signal that a process sends itself is decided under sig_lock
before it is queued. The killdfl and killchild late cases and the
default kill cases of test-sigchld-after-wait pass on Linux 6.18 too.

A process that has not republished yet, such as a fork child that has
not changed its SIGCHLD action or mask, has no flag file, so a kill
from another process to it is still decided at the drain, which
rt_sigaction runs before it drops a pending SIGCHLD. A kill to a traced
target with SIGCHLD unblocked, which sig_ignored keeps under SIG_IGN or
SIG_DFL, is discarded here (by reading, not run).
Comment thread src/syscall/proc.c
uint64_t rusage_gva)
{
int64_t rc = wait4_once(g, pid, status_gva, options, rusage_gva);
if (rc > 0 || rc == -LINUX_EFAULT || signal_sigchld_autoreap())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Under auto-reap this drains on every return, WNOHANG included. A drain that reads a SIGCHLD record calls proc_autoreap_exited_children, whose host wait4(host_pid, &status, 0, &ru) blocks until the child's host process is gone. The exiting child appends its record before it runs proc_notify_reparent, which retries for up to 50 ms per live child it hands to the adopter. So waitpid(-1, NULL, WNOHANG) under SIG_IGN can stall for that long, where Linux returns ECHILD at once. The drain at the top of rt_sigaction does the same when the old action auto-reaps (SIG_IGN to SIG_DFL).

Before this change only the preempt thread paid for that host wait. When the drain runs on a syscall path, have its auto-reap use WNOHANG and leave a child whose host process has not exited yet to the preempt thread's drain.

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