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 <john@hawthorn.email>
This commit is contained in:
Aaron Patterson 2025-03-25 15:55:33 -07:00 committed by John Hawthorn
parent f7ff380998
commit e3452cfad2
Notes: git 2025-05-08 17:53:41 +00:00
2 changed files with 45 additions and 0 deletions

View File

@ -2213,7 +2213,49 @@ assert_equal 'ok', %q{
# fork after creating Ractor # fork after creating Ractor
assert_equal 'ok', %q{ assert_equal 'ok', %q{
begin
Ractor.new { Ractor.receive } Ractor.new { Ractor.receive }
_, status = Process.waitpid2 fork { } _, status = Process.waitpid2 fork { }
status.success? ? "ok" : status 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
} }

View File

@ -2096,6 +2096,9 @@ rb_ractor_terminate_atfork(rb_vm_t *vm, rb_ractor_t *r)
rb_gc_ractor_cache_free(r->newobj_cache); rb_gc_ractor_cache_free(r->newobj_cache);
r->newobj_cache = NULL; r->newobj_cache = NULL;
r->status_ = ractor_terminated; 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 #endif