Skip to content

Commit

Permalink
[Bug #20995] Protect IO.popen block from exiting by exception
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Jan 2, 2025
1 parent 6cf11ad commit 8034e9c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion io.c
Original file line number Diff line number Diff line change
Expand Up @@ -8038,7 +8038,7 @@ popen_finish(VALUE port, VALUE klass)
if (NIL_P(port)) {
/* child */
if (rb_block_given_p()) {
rb_yield(Qnil);
rb_protect(rb_yield, Qnil, NULL);
rb_io_flush(rb_ractor_stdout());
rb_io_flush(rb_ractor_stderr());
_exit(0);
Expand Down
30 changes: 22 additions & 8 deletions test/ruby/test_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -922,15 +922,29 @@ def test_execopts_popen_extra_fd
}
end

def test_popen_fork
IO.popen("-") {|io|
if !io
puts "fooo"
else
assert_equal("fooo\n", io.read)
if Process.respond_to?(:fork)
def test_popen_fork
IO.popen("-") do |io|
if !io
puts "fooo"
else
assert_equal("fooo\n", io.read)
end
end
}
rescue NotImplementedError
end

def test_popen_fork_ensure
IO.popen("-") do |io|
if !io
STDERR.reopen(STDOUT)
raise "fooo"
else
assert_empty io.read
end
end
rescue RuntimeError
abort "[Bug #20995] should not reach here"
end
end

def test_fd_inheritance
Expand Down

0 comments on commit 8034e9c

Please sign in to comment.