Skip to content

Commit 4857fcc

Browse files
authored
Delete subprocess output tempfiles once they are read (sds#901)
Fixes sds#899 `Subprocess.spawn` captures the child's stdout and stderr in two Tempfiles and never closes them, so deleting them is left to the garbage collector. On Windows that delete fails with `Errno::EACCES` when a process left behind by the command (for example the inner `ruby.exe` of `bundle exec rspec`) still has the file open, and Ruby prints the finalizer backtrace from the issue on every hook run. This closes and unlinks both files (`Tempfile#close!`) in an `ensure` once the output has been read, so they are gone when `spawn` returns, including when reading the output raises. As suggested in the issue, this goes through `Tempfile#unlink` rather than `File.unlink`, so a delete that Windows refuses is ignored instead of raising. `spawn_detached` is unchanged: its child is still running when it returns, so its files are still left to the garbage collector. ### Tests Two examples in `spec/overcommit/subprocess_spec.rb` record the Tempfiles that `spawn` creates and check that both are closed and unlinked (`Tempfile#path` is `nil`) after it returns: one for a normal run, one where reading the output raises. The suite was run on macOS with Ruby 4.0.7: - `bundle exec rspec spec/overcommit/subprocess_spec.rb` with `lib/overcommit/subprocess.rb` reverted to master: `10 examples, 2 failures`, both on `expected ... closed? to be truthy, got false`. With the change: `10 examples, 0 failures`. - `bundle exec rspec`: `1657 examples, 4 failures`. The same 4 examples fail on master (`1655 examples, 4 failures`): the git alias `#amendment?` examples in `hook_context/commit_msg_spec.rb:134,140` and `hook_context/pre_commit_spec.rb:43,49`, which look specific to this machine's Git (Apple Git 2.54). - `bundle exec rubocop`: no offenses. Not run: anything on Windows, so the `Errno::EACCES` warning itself was not reproduced; `bundle exec overcommit --run`. Co-authored-by: ybai08 <[email protected]>
1 parent 55ea3fd commit 4857fcc

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎lib/overcommit/subprocess.rb‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ def spawn(args, options = {})
5050
out.rewind
5151

5252
Result.new(process.exit_code, out.read, err.read)
53+
ensure
54+
# Delete the output files now rather than leaving it to the garbage
55+
# collector, which on Windows fails (and prints a backtrace) if a process
56+
# left behind by the command still has them open.
57+
[out, err].each { |stream| stream&.close! }
5358
end
5459

5560
# Spawns a new process in the background using the given array of

‎spec/overcommit/subprocess_spec.rb‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,53 @@ def received_argv(result)
106106
received_argv(subject).should == ['', 'after']
107107
end
108108
end
109+
110+
context 'once the process has finished' do
111+
# Regression test for https://github.com/sds/overcommit/issues/899, where
112+
# the temporary files capturing the output were left for the garbage
113+
# collector to delete. On Windows that delete fails with `Errno::EACCES`
114+
# when a process left behind by the command still holds the file open,
115+
# and Ruby prints a backtrace from the finalizer on every hook run.
116+
let(:tempfiles) { [] }
117+
118+
# Records every Tempfile created, passing each one to the block (if given)
119+
# so a test can stub it.
120+
def record_tempfiles(&block)
121+
::Tempfile.stub(:new).and_wrap_original do |original, *args|
122+
original.call(*args).tap do |tempfile|
123+
tempfiles << tempfile
124+
block&.call(tempfile)
125+
end
126+
end
127+
end
128+
129+
def expect_tempfiles_deleted
130+
tempfiles.size.should == 2
131+
tempfiles.each do |tempfile|
132+
tempfile.should be_closed
133+
tempfile.path.should be_nil # Tempfile#path is nil once unlinked
134+
end
135+
end
136+
137+
it 'closes and deletes the files that captured its output' do
138+
record_tempfiles
139+
140+
result = described_class.spawn([ruby, '-e', 'STDOUT.print "out"; STDERR.print "err"'])
141+
142+
result.stdout.should == 'out'
143+
result.stderr.should == 'err'
144+
expect_tempfiles_deleted
145+
end
146+
147+
it 'closes and deletes the files when reading the output fails' do
148+
record_tempfiles { |tempfile| tempfile.stub(:read).and_raise(IOError, 'read failed') }
149+
150+
expect { described_class.spawn([ruby, '-e', 'STDOUT.print "out"']) }.
151+
to raise_error(IOError, 'read failed')
152+
153+
expect_tempfiles_deleted
154+
end
155+
end
109156
end
110157

111158
# Subprocess no longer branches on platform, but this guards against

0 commit comments

Comments
 (0)