Skip to content

Commit c0acd7a

Browse files
committed
Avoid closing directory we're iterating
Ruby 3.4 started error checking directory access and starts to raise Errno::EBADF. This particular loop iterates on all open file descriptors and one is the directory listing from Dir.foreach. In the past this could have led to leaked file descriptors, but it's unlikely since it's likely the last opened file descriptor and have the highest number. Link: ruby/ruby@f2919bd Link: https://bugzilla.redhat.com/show_bug.cgi?id=2349352
1 parent d27a455 commit c0acd7a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

lib/puppet/util.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,10 @@ def safe_posix_fork(stdin = $stdin, stdout = $stdout, stderr = $stderr, &block)
478478
$stderr = STDERR
479479

480480
begin
481-
Dir.foreach('/proc/self/fd') do |f|
482-
if f != '.' && f != '..' && f.to_i >= 3
481+
d = Dir.new('/proc/self/fd')
482+
ignore_fds = ['.', '..', d.fileno.to_s]
483+
d.each_child do |f|
484+
if !ignore_fds.include?(f) && f.to_i >= 3
483485
begin
484486
IO.new(f.to_i).close
485487
rescue

0 commit comments

Comments
 (0)