Skip to content

Stop leaking the child process and its pipes when execute fails or is interrupted - #18

Merged
orien merged 1 commit into
masterfrom
close-descriptors-and-reap
Nov 22, 2026
Merged

orien merged 1 commit into
masterfrom
close-descriptors-and-reap

Conversation

@orien

@orien orien commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Context

SafeShell.execute runs a command in a child process and reads its output through a pipe. It handles the normal case correctly and has no error handling at all. This follows #16, which gave the child an empty stdin so that commands reading it no longer hang.

The files it opens for the :stdout and :stderr options are never closed in the parent, so every redirected call leaks two file handles until garbage collection. If starting the command fails, for a missing executable say, both ends of the output pipe leak too. And if the read of the output is interrupted, by Timeout.timeout or a signal, the parent never waits for the child: the process is left running or as a zombie, the pipe stays open, and $?, the exit status Ruby exposes for the last child, still describes an earlier command, so a later $?.success? answers for the wrong one.

Change

execute gains a cleanup path that runs however it exits. It closes everything it opened, and if the child is still running it stops it and waits for it, so a caller is never left with leaked handles, an abandoned child, or a $? for the wrong command.

  • The body of execute runs inside begin/ensure, and the ensure closes both pipe ends and any redirect files before anything else.
  • If the child is still running, it is sent TERM, given a second to exit, then sent KILL and given another second. The wait is measured on the monotonic clock, so it cannot be stretched or shortened by a clock change or by another thread.
  • A child that has already exited is waited for without being signalled. One that cannot be signalled, or that something else in the process has already waited for, is left alone.
  • The interrupts Ruby can defer are held for the whole call except around the read and the wait, so the cleanup always knows the child's pid and the open handles, and a second interruption cannot abandon it part way.
  • On CRuby an exception raised from a signal handler installed with trap cannot be deferred. If one lands during the cleanup, the child is killed and waited for before the exception continues.
  • The README documents this behaviour and the cases outside it.
  • The specs' "resource handling" context has an example for each path above, including the engine differences that CI found on JRuby and TruffleRuby.

Considerations

  • This changes behaviour for callers who wrap execute in Timeout.timeout: the command used to keep running after the timeout and is now stopped.
  • An interrupted call returns up to a second after the interruption, or two if the child ignores TERM, because it waits for the child before returning.
  • Redirect files stay open read-write, because opening a FIFO write-only blocks until something reads it.
  • The specs that count open handles read /dev/fd and are skipped where it does not exist.

@orien
orien force-pushed the close-descriptors-and-reap branch 4 times, most recently from 8543008 to 0a4b14d Compare November 22, 2026 10:40
@orien orien changed the title Close descriptors and reap the child on every path Always clean up the child process and its pipes when execute exits Nov 22, 2026
@orien orien changed the title Always clean up the child process and its pipes when execute exits Stop leaking the child process and its pipes when execute fails or is interrupted Nov 22, 2026
execute opened redirect files and never closed them in the parent, and
if spawn raised, both pipe ends leaked too. If the read of the child's
output was interrupted, by a timeout or a signal, waitpid never ran: the
child was never waited on, the pipe stayed open, and $? still described
the previous command.

Hold the interrupts Ruby can defer across the whole call except the
read and the wait, so the ensure always has the pid and the descriptors.
The ensure closes each descriptor in its own rescue, then terminates
and reaps a child still running: TERM, a one-second grace period, KILL,
another second, each bounded on the monotonic clock. A child that has
already exited is reaped without being signalled; one that cannot be
signalled, or that something else has reaped, is left. On CRuby an
exception raised from a trap handler is not deferred, so one landing in
the cleanup kills and reaps the child before being re-raised. JRuby
delivers it after the cleanup instead.

Redirect files stay open read-write: write-only blocks on a FIFO with
no reader.

Specs cover descriptor counts on every path and $? after interruption
during the read, during spawn, with a child that ignores TERM, that
does not die on KILL, that cannot be signalled, that has exited or been
reaped, with a second timeout, with a trap handler raising once and
twice, and with the waiting thread woken early. The README describes
the behaviour and its limits.
@orien
orien force-pushed the close-descriptors-and-reap branch from 0a4b14d to dc8816b Compare November 22, 2026 10:59
@orien
orien merged commit c11e1ab into master Nov 22, 2026
16 checks passed
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