From ab637cad2b582e8247bafd87a3b0f6323d564f64 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Wed, 20 Sep 2023 21:32:40 +0900 Subject: [PATCH] [Bug #19624] Clean up backquote IO It should not be hidden, since it can be grabbed by a fiber scheduler. --- io.c | 20 +++++++++++--------- test/fiber/test_io.rb | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/io.c b/io.c index 712dce3ceb89c4..48cdc5b9a7aca4 100644 --- a/io.c +++ b/io.c @@ -5574,12 +5574,9 @@ clear_codeconv(rb_io_t *fptr) clear_writeconv(fptr); } -void -rb_io_fptr_finalize_internal(void *ptr) +static void +rb_io_fptr_cleanup_all(rb_io_t *fptr) { - rb_io_t *fptr = ptr; - - if (!ptr) return; fptr->pathv = Qnil; if (0 <= fptr->fd) rb_io_fptr_cleanup(fptr, TRUE); @@ -5587,7 +5584,14 @@ rb_io_fptr_finalize_internal(void *ptr) free_io_buffer(&fptr->rbuf); free_io_buffer(&fptr->wbuf); clear_codeconv(fptr); - free(fptr); +} + +void +rb_io_fptr_finalize_internal(void *ptr) +{ + if (!ptr) return; + rb_io_fptr_cleanup_all(ptr); + free(ptr); } #undef rb_io_fptr_finalize @@ -10467,11 +10471,9 @@ rb_f_backquote(VALUE obj, VALUE str) if (NIL_P(port)) return rb_str_new(0,0); GetOpenFile(port, fptr); - rb_obj_hide(port); result = read_all(fptr, remain_size(fptr), Qnil); rb_io_close(port); - RFILE(port)->fptr = NULL; - rb_io_fptr_finalize(fptr); + rb_io_fptr_cleanup_all(fptr); RB_GC_GUARD(port); return result; diff --git a/test/fiber/test_io.rb b/test/fiber/test_io.rb index c1ad56a8cfe25a..0e3e086d5aee52 100644 --- a/test/fiber/test_io.rb +++ b/test/fiber/test_io.rb @@ -219,4 +219,19 @@ def test_io_select assert_equal [[r], [w], []], result end end + + def test_backquote + result = nil + + thread = Thread.new do + scheduler = Scheduler.new + Fiber.set_scheduler scheduler + Fiber.schedule do + result = `#{EnvUtil.rubybin} -e "sleep 0.1;puts %[ok]"` + end + end + thread.join + + assert_equal "ok\n", result + end end