Drop SIGCHLD that trails a reaped child

Closed
XA
3d ago
+1036-140

Intent: A child's SIGCHLD exists by the time a parent's wait4 or waitid can return that child, as on Linux, so a SIGCHLD handler installed after the reap never runs for it.

Failure fixed: An exiting child saved its exit to the lifecycle registry, released the registry lock, and only then appended SIGCHLD to the parent's signal transport. A parent wait4 that read the registry in between returned the child, and the SIGCHLD arrived later. lmbench's benchmp reaps under SIG_DFL, installs a SIGCHLD handler, and forks the next child; the late signal made it SIGTERM a healthy child that ignores SIGTERM, and lat_fs waited until the CI timeout (#498). On Linux, SIGCHLD is generated with the zombie, so under SIG_DFL it is discarded before wait returns.

This PR: The exiting child appends its SIGCHLD records before releasing the registry lock and sends SIGUSR2 after. wait4 and waitid drain the caller's transport before returning, so the signal is queued under the disposition the wait ran with and the syscall epilogue discards it under SIG_DFL or SIG_IGN. The drain now reads and truncates the file under its flock and handles the records after releasing it, because a record handler takes the registry lock that the exiting child holds while appending. A new transport_drain_lock serializes whole drains and is added to the lock-order list. One commit.

Verification:

  • lat_fs -N 1 -s 1024 in the bench suite's command form (sh -c '"$0" "$@"; exit $?'), four concurrent loops beside ten busy processes: 4 of 96 runs timed out before, 0 of 96 after. With a 150 ms sleep added before the SIGCHLD send: 24 of 24 timed out before, 0 of 24 after.
  • New test-sigchld-after-wait (registered in tests/test-matrix.sh): 20 rounds of reaping a child under SIG_DFL, installing a handler and watching for 100 ms. It passes under elfuse and on Linux 6.18 (QEMU), and with the 150 ms sleep it fails 20 of 20 rounds before the fix.
  • make check passes. make test-matrix: both aarch64 lanes pass with no failures; the one failing case is Rosetta audit-known-limitations, which fails the same way on upstream/main.
  • fork + waitpid latency: about 5.4 ms per iteration before and after.

Not tested: the adopter SIGCHLD for a reparented, already-exited child (now also appended under the registry lock) has no test that checks its timing, and the drain's read and truncate failure branches need fault injection.

Fixes #498


Summary by cubic

Drops a SIGCHLD that arrives after the child was already reaped, which made lmbench's lat_fs time out in CI (#498). An exiting child now appends its SIGCHLD before releasing the registry lock, and wait4/waitid drain the caller's transport before returning, so the signal is judged under the disposition the wait ran with and dropped under SIG_DFL or SIG_IGN unless a thread blocks it. The auto-reap a drained record starts no longer waits for the child's host process to exit; the child leaves the guest table at once and the deferred-reap sweep reaps it later. An adopter that gains several zombies at once gets one merged SIGCHLD record instead of one per zombie.

  • The drain reads in 8 KiB chunks from an offset, holds its flock only per read, handles each chunk's records after releasing it, and compacts the unread tail forward instead of truncating when a sender keeps appending; a failed open or read re-arms the doorbell so the SIGALRM tick retries it once per tick. The doorbell also broadcasts on pid_cond so a waiting parent re-checks immediately. Failed reads and failed truncates or compactions each log once per run of failures.
  • Adds test-sigchld-after-wait (reaping under SIG_DFL and SIG_IGN through both waitpid and waitid, 20 rounds each), plus test-signal-backlog, test-record-split-host, and test-file-compact-host.