merge revision(s) 8034e9c3d0: [Backport #20995]

[Bug #20995] Protect `IO.popen` block from exiting by exception
This commit is contained in:
Takashi Kokubun 2025-02-13 18:01:20 -08:00
parent c989d90754
commit b65cea7429
3 changed files with 24 additions and 10 deletions

2
io.c
View file

@ -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);

View file

@ -922,15 +922,29 @@ class TestProcess < Test::Unit::TestCase
}
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

View file

@ -11,7 +11,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 1
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
#define RUBY_PATCHLEVEL 10
#define RUBY_PATCHLEVEL 11
#include "ruby/version.h"
#include "ruby/internal/abi.h"