Repository navigation
Stop leaking the child process and its pipes when execute fails or is interrupted - #18
Merged
Merged
Conversation
orien
force-pushed
the
close-descriptors-and-reap
branch
4 times, most recently
from
November 22, 2026 10:40
8543008 to
0a4b14d
Compare
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
force-pushed
the
close-descriptors-and-reap
branch
from
November 22, 2026 10:59
0a4b14d to
dc8816b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
SafeShell.executeruns 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
:stdoutand:stderroptions 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, byTimeout.timeoutor 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
executegains 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.executeruns insidebegin/ensure, and theensurecloses both pipe ends and any redirect files before anything else.TERM, given a second to exit, then sentKILLand 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.trapcannot be deferred. If one lands during the cleanup, the child is killed and waited for before the exception continues.Considerations
executeinTimeout.timeout: the command used to keep running after the timeout and is now stopped.TERM, because it waits for the child before returning./dev/fdand are skipped where it does not exist.