Skip to content

Avoid closing directory we're iterating #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions lib/puppet/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,15 @@ def safe_posix_fork(stdin = $stdin, stdout = $stdout, stderr = $stderr, &block)
$stderr = STDERR

begin
Dir.foreach('/proc/self/fd') do |f|
if %{^\d+$}.match?(f) && f.to_i >= 3
begin
IO.new(f.to_i).close
rescue
nil
end
d = Dir.new('/proc/self/fd')
ignore_fds = ['.', '..', d.fileno.to_s]
d.each_child do |f|
next if ignore_fds.include?(f) || f.to_i < 3
Comment on lines +482 to +484
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at #41 I wonder if we should explicitly exclude the FDs that we want to keep open:

Suggested change
ignore_fds = ['.', '..', d.fileno.to_s]
d.each_child do |f|
next if ignore_fds.include?(f) || f.to_i < 3
ignore_fds = ['.', '..', stdin.fileno.to_s, stdout.fileno.to_s, stderr.fileno.to_s, d.fileno.to_s]
d.each_child do |f|
next if ignore_fds.include?(f)

This version is probably safer. Not sure if it's valid to have no stdin, stdout or stderr.

Suggested change
ignore_fds = ['.', '..', d.fileno.to_s]
d.each_child do |f|
next if ignore_fds.include?(f) || f.to_i < 3
ignore_fds = ['.', '..'] + [stdin, stdout, stderr, d].filter_map { |f| f&.fileno&.to_s }
d.each_child do |f|
next if ignore_fds.include?(f)

What makes me hesitant is that I don't know

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little worried we're playing whack-a-mole with things we want to filter out and I'm not sure how to be sure we're being exhaustive about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this is much better than looking at .to_i < 3 and assuming that those file descriptors are stdin, stdout and stderr since it is explicit about what's going on.


begin
IO.new(f.to_i).close
rescue
nil
end
end
rescue Errno::ENOENT, Errno::ENOTDIR # /proc/self/fd not found, /proc/self not a dir
Expand Down
12 changes: 7 additions & 5 deletions spec/unit/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -616,18 +616,20 @@ def withenv_utf8(&block)
fds.each do |fd|
if fd == '.' || fd == '..'
next
elsif ['0', '1', '2'].include? fd
elsif ['0', '1', '2', '5'].include? fd
expect(IO).not_to receive(:new).with(fd.to_i)
else
expect(IO).to receive(:new).with(fd.to_i).and_return(double('io', close: nil))
end
end

dir_expectation = receive(:foreach).with('/proc/self/fd')
dir = double(Dir, fileno: '5')
dir_expectation = receive(:each_child)
fds.each do |fd|
dir_expectation = dir_expectation.and_yield(fd)
end
allow(Dir).to dir_expectation
allow(dir).to dir_expectation
allow(Dir).to receive(:new).with('/proc/self/fd').and_return(dir)
Puppet::Util.safe_posix_fork
end

Expand All @@ -636,7 +638,7 @@ def withenv_utf8(&block)
# letting it actually close fds, which seems risky
(0..2).each {|n| expect(IO).not_to receive(:new).with(n)}
(3..256).each {|n| expect(IO).to receive(:new).with(n).and_return(double('io', close: nil)) }
allow(Dir).to receive(:foreach).with('/proc/self/fd').and_raise(Errno::ENOENT)
allow(Dir).to receive(:new).with('/proc/self/fd').and_raise(Errno::ENOENT)

Puppet::Util.safe_posix_fork
end
Expand All @@ -646,7 +648,7 @@ def withenv_utf8(&block)
# letting it actually close fds, which seems risky
(0..2).each {|n| expect(IO).not_to receive(:new).with(n)}
(3..256).each {|n| expect(IO).to receive(:new).with(n).and_return(double('io', close: nil)) }
allow(Dir).to receive(:foreach).with('/proc/self/fd').and_raise(Errno::ENOTDIR)
allow(Dir).to receive(:new).with('/proc/self/fd').and_raise(Errno::ENOTDIR)

Puppet::Util.safe_posix_fork
end
Expand Down