Skip to content

Don't signal the pid of a command reaped during the wait - #37

Merged
orien merged 1 commit into
mainfrom
echild-no-signal
Nov 29, 2026
Merged

orien merged 1 commit into
mainfrom
echild-no-signal

Conversation

@orien

@orien orien commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Context

When something else in the process reaps the command first, the wait after the read raises Errno::ECHILD. The command's pid is still set at that point, so the cleanup calls terminate_and_reap on it. If the OS has already given that pid to another child of the process, the cleanup finds that child running and sends it TERM, then KILL, and reaps it.

PE-8869

Change

Clears the pid before re-raising the ECHILD, so the cleanup leaves it alone.

The same can happen when the call is interrupted after something else has reaped the command, during the read or during the grace period after TERM. Ruby has no way to tell a reaped command from a child that was given its pid, so the README and the docs on execute now say a reused pid's new owner can be signalled in that case.

Considerations

  • The window is small: the pid has to be reused between the other waiter reaping the command and the cleanup running.
  • The example simulates the reuse by stubbing Process.waitpid: the blocking wait reaps the command and raises, and the cleanup's non-blocking wait reports a running child. It fails on main with a TERM sent to that pid.

When the wait after the read raises Errno::ECHILD, something else in
the process has reaped the command. pid was still set, so the ensure
called terminate_and_reap on it. If the pid had already been given to
another child of the process, its WNOHANG wait found that child
running, and SafeShell sent it TERM, then KILL, and reaped it.

Clear pid before re-raising, so the ensure leaves it alone.

The same can happen when the call is interrupted after something else
has reaped the command, during the read or the grace period: nothing
can tell a reaped command from a child that was given its pid. Say so
in the README and the docs on execute.
@orien orien changed the title Don't signal the command's pid after something else reaps it Don't signal the pid of a command reaped during the wait Nov 29, 2026
@orien
orien marked this pull request as ready for review November 29, 2026 15:07
@orien
orien merged commit 2824090 into main Nov 29, 2026
16 checks passed
@orien
orien deleted the echild-no-signal branch November 29, 2026 15:08
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.

1 participant