Skip to content

Commit e0fac59

Browse files
Fix interrupt pass reporting a group gone before its leader is reaped (#2088) (#2218)
* Start fix for interrupt pass leader reap race Refs #2088. Assisted-by: Claude Code:claude-fable-5-2 * Test an interrupt pass over an unreaped leader A pass of the interrupt handler over a build's process group says the group is gone when the group probe finds no member, whether or not the build's own child has been reaped. The new test puts a pass in that state with a child that leads no group, and fails. Refs #2088. Assisted-by: Claude Code:claude-fable-5-2 * Reap a build's leader before calling it gone An interrupt pass called a build's process group gone as soon as the group probe found no member. macOS stops counting an exiting process in its group before its parent can wait for it, so a pass could say so with the group's leader not yet waitable, and leave the leader unreaped. The nightly macOS run failed on that in an_interrupt_pass_ends_the_whole_build_group. A pass now calls the group gone only once a wait for the leader has answered: reaped by this pass, or already reaped by an earlier pass or by the build's own thread. Fixes #2088. Assisted-by: Claude Code:claude-fable-5-2 --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent bd5b8d5 commit e0fac59

1 file changed

Lines changed: 53 additions & 9 deletions

File tree

‎crates/socket-patch-core/src/utils/process.rs‎

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,13 @@ pub fn kill_builds_on_interrupt() {
424424

425425
/// One pass of [`kill_builds_on_interrupt`] over the process group `pgid`:
426426
/// SIGKILL it (once: `killed` remembers the group), reap its leader when
427-
/// it has exited, and say whether no process of the group is left.
428-
/// Async-signal-safe.
427+
/// it has exited, and say whether no process of the group is left and the
428+
/// leader is reaped. Async-signal-safe.
429+
///
430+
/// The leader is this process's own child, so a wait for it is the one
431+
/// certain sign that it is dead. The group probe alone is not: macOS stops
432+
/// counting an exiting process in its group before its parent can wait
433+
/// for it, so the probe can find nobody while the leader is still going.
429434
#[cfg(unix)]
430435
fn end_build_group(pgid: i32, killed: &mut i32) -> bool {
431436
// SAFETY: plain syscalls on a process group this process created and
@@ -435,15 +440,17 @@ fn end_build_group(pgid: i32, killed: &mut i32) -> bool {
435440
libc::kill(-pgid, libc::SIGKILL);
436441
*killed = pgid;
437442
}
443+
// `waitpid(WNOHANG)` answers 0 only for a child that has not
444+
// exited yet. Any other answer means the leader is reaped: by
445+
// this call, or (ECHILD) by an earlier pass or the build's thread.
438446
let mut status = 0;
439-
libc::waitpid(pgid, &mut status, libc::WNOHANG);
447+
let mut reaped = libc::waitpid(pgid, &mut status, libc::WNOHANG) != 0;
440448
let gone = libc::kill(-pgid, 0) != 0;
441-
if gone {
442-
// Where a zombie no longer counts as a member of its group
443-
// (macOS), the leader may have died since the wait above.
444-
libc::waitpid(pgid, &mut status, libc::WNOHANG);
449+
if gone && !reaped {
450+
// The leader may have died since the wait above.
451+
reaped = libc::waitpid(pgid, &mut status, libc::WNOHANG) != 0;
445452
}
446-
gone
453+
gone && reaped
447454
}
448455
}
449456

@@ -1229,7 +1236,44 @@ pub(crate) mod tests {
12291236
assert!(gone, "the build's compiler {compiler} is still there");
12301237
assert!(
12311238
child.try_wait().is_err(),
1232-
"the pass reaped the build's child itself"
1239+
"the pass left the build's child unreaped"
1240+
);
1241+
}
1242+
1243+
/// A pass does not call a group gone while the build's own child is
1244+
/// still to be reaped. macOS stops counting an exiting process in its
1245+
/// group before its parent can wait for it; a child that leads no
1246+
/// group puts a pass in the same state on every Unix: the group probe
1247+
/// finds nobody, and the child is not waitable.
1248+
#[cfg(unix)]
1249+
#[test]
1250+
fn an_interrupt_pass_waits_for_the_leader_the_group_probe_missed() {
1251+
let mut build = Command::new("sleep");
1252+
build
1253+
.arg("30")
1254+
.stdin(Stdio::null())
1255+
.stdout(Stdio::null())
1256+
.stderr(Stdio::null());
1257+
let mut child = build.spawn().unwrap();
1258+
let pid = i32::try_from(child.id()).unwrap();
1259+
1260+
let mut killed = 0;
1261+
let gone = end_build_group(pid, &mut killed);
1262+
// The child is in the test's group, so the pass did not signal it.
1263+
child.kill().unwrap();
1264+
assert!(!gone, "the pass called a group gone over its live leader");
1265+
1266+
let start = Instant::now();
1267+
while !end_build_group(pid, &mut killed) {
1268+
assert!(
1269+
start.elapsed() < Duration::from_secs(10),
1270+
"the killed child {pid} was never reaped"
1271+
);
1272+
std::thread::sleep(Duration::from_millis(5));
1273+
}
1274+
assert!(
1275+
child.try_wait().is_err(),
1276+
"the pass called the group gone with its leader unreaped"
12331277
);
12341278
}
12351279

0 commit comments

Comments
 (0)