Skip to content

Fix interrupt pass reporting a group gone before its leader is reaped (#2088) - #2218

Merged
Mikola Lysenko (mikolalysenko) merged 3 commits into
mainfrom
agent/fix-interrupt-pass-leader-reap
Nov 24, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 3 commits into
mainfrom
agent/fix-interrupt-pass-leader-reap

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

LLM Description written by Claude Code:claude-fable-5-2

Fixes #2088

Summary

The nightly macOS run fails now and then on
utils::process::tests::an_interrupt_pass_ends_the_whole_build_group
("the pass reaped the build's child itself"). The test is right and the code
under it has a race: a pass of the interrupt handler can report a build's
process group gone while the group's leader has not been reaped. This PR
makes a pass wait for the leader, and adds a test that shows the race on
every Unix, not only on macOS.

One file changes: crates/socket-patch-core/src/utils/process.rs (8 lines
of code in end_build_group, one new test, one clearer assert message).

Root cause

end_build_group sends SIGKILL to the group, calls waitpid(WNOHANG) on
the leader, probes the group with kill(-pgid, 0), and returned whatever
the probe said. It never looked at what waitpid answered. macOS stops
counting an exiting process as a member of its group before its parent can
wait for it. In that window the probe finds nobody, both waits answer 0
("not exited yet"), and the pass says "gone" with the leader unreaped. The
test then finds the child still waitable.

That reading of macOS comes from the failure itself (the assertion can only
fail if a pass returned true without a successful wait); I could not run
macOS here.

Fix

A pass now reports the group gone only when the probe finds nobody and a
wait for the leader has answered something other than 0: reaped by this
pass, or already reaped (ECHILD) by an earlier pass or by the build's own
thread. The leader is this process's own child, so the wait is the one
certain sign that it is dead. kill_builds_on_interrupt still gives up
after its 400 passes (about 2 s), so the handler cannot hang.

Behavior for users: on an interrupt, the apply lock is released slightly
later on macOS in the rare window above (a few milliseconds), never earlier
than before.

Evidence

  • New test an_interrupt_pass_waits_for_the_leader_the_group_probe_missed
    (covers Flake: an_interrupt_pass_ends_the_whole_build_group fails on macOS ("the pass reaped the build's child itself") #2088). It puts a pass in the same state on Linux with a child
    that leads no group: the probe finds nobody and the child is not waitable.
    • On the test-only commit b3aab38 it fails:
      the pass called a group gone over its live leader.
    • With the fix f764d43 it passes.
  • cargo test -p socket-patch-core --lib an_interrupt_pass repeated 30
    times: 30 passes.
  • cargo test -p socket-patch-core: all pass (8357 in the lib).
  • cargo clippy --locked -p socket-patch-core -p socket-patch-cli --all-features -- -D warnings
    (CI's form): clean.
  • an_interrupt_pass_ends_the_whole_build_group only flakes on macOS, so
    the macOS test job on this PR and the next nightly runs are the real
    confirmation there.

Review notes (/code-review high)

No finding against this change. It reported three things that were already
true before it, which I left alone to keep this PR to one cause:

  • The group probe treats any kill(-pgid, 0) failure as "nobody left",
    EPERM included (a member running as another user).
  • A build's slot keeps its group id while output is still being collected
    after the leader was reaped, so in theory the pid could be recycled.
  • On macOS a non-leader member (the compiler) could likewise be mid-exit
    and uncounted when the pass reports the group gone. It has been sent
    SIGKILL by then.

And one nit, not taken: waitpid returning -1 is read as "reaped" without
checking that errno is ECHILD. With WNOHANG no other error is
reachable for a pid this process spawned.

https://claude.ai/code/session_011f9aeRgAtMGHGSBTPPnhKe


Generated by Claude Code

Refs #2088.

Assisted-by: Claude Code:claude-fable-5-2
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
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
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 24, 2026 14:01
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches async-signal-safe interrupt teardown for build process groups; incorrect waiting could delay lock release or leave zombies, but the change narrows the “gone” condition rather than loosening cleanup.

Overview
Fixes a race in end_build_group (used by kill_builds_on_interrupt): a pass no longer treats a build’s process group as finished when kill(-pgid, 0) alone says nobody is left. It now requires both an empty group probe and that the leader child was reaped via waitpid(WNOHANG), with a second wait if the probe turned empty before the first wait succeeded—addressing macOS timing where an exiting leader drops out of the group count before the parent can wait.

Adds an_interrupt_pass_waits_for_the_leader_the_group_probe_missed to lock in that behavior on all Unix, and clarifies an assertion in the existing interrupt-pass test.

Reviewed by Cursor Bugbot for commit f764d43. Configure here.

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.

[agent] Reviewed the interrupt loop and the regression where the group probe cannot see a live child. The pass now requires both conditions, with the existing bounded retry loop.

Merged via the queue into main with commit e0fac59 Nov 24, 2026
83 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the agent/fix-interrupt-pass-leader-reap branch November 24, 2026 16:21
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.

Flake: an_interrupt_pass_ends_the_whole_build_group fails on macOS ("the pass reaped the build's child itself")

3 participants