From e3452cfad26edcb3ba6e9bf818f7d02838cf676f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 25 Mar 2025 15:55:33 -0700 Subject: [PATCH] Raise error on take/send for Ractors in child processes Ractor objects that are available in a child process should raise a `Ractor::ClosedError` exception when called with `send` or `take` Co-authored-by: John Hawthorn --- bootstraptest/test_ractor.rb | 42 ++++++++++++++++++++++++++++++++++++ ractor.c | 3 +++ 2 files changed, 45 insertions(+) diff --git a/bootstraptest/test_ractor.rb b/bootstraptest/test_ractor.rb index 94c0635c72..19a6e16302 100644 --- a/bootstraptest/test_ractor.rb +++ b/bootstraptest/test_ractor.rb @@ -2213,7 +2213,49 @@ assert_equal 'ok', %q{ # fork after creating Ractor assert_equal 'ok', %q{ +begin Ractor.new { Ractor.receive } _, status = Process.waitpid2 fork { } status.success? ? "ok" : status +rescue NotImplementedError + :ok +end +} + +# Ractors should be terminated after fork +assert_equal 'ok', %q{ +begin + r = Ractor.new { Ractor.receive } + _, status = Process.waitpid2 fork { + begin + r.take + raise "ng" + rescue Ractor::ClosedError + end + } + r.send(123) + raise unless r.take == 123 + status.success? ? "ok" : status +rescue NotImplementedError + :ok +end +} + +# Ractors should be terminated after fork +assert_equal 'ok', %q{ +begin + r = Ractor.new { Ractor.receive } + _, status = Process.waitpid2 fork { + begin + r.send(123) + raise "ng" + rescue Ractor::ClosedError + end + } + r.send(123) + raise unless r.take == 123 + status.success? ? "ok" : status +rescue NotImplementedError + :ok +end } diff --git a/ractor.c b/ractor.c index c536d27e4e..c6095c8863 100644 --- a/ractor.c +++ b/ractor.c @@ -2096,6 +2096,9 @@ rb_ractor_terminate_atfork(rb_vm_t *vm, rb_ractor_t *r) rb_gc_ractor_cache_free(r->newobj_cache); r->newobj_cache = NULL; r->status_ = ractor_terminated; + r->sync.outgoing_port_closed = true; + r->sync.incoming_port_closed = true; + r->sync.will_basket.type.e = basket_type_none; } #endif