Skip to content

Commit e3452cf

Browse files
tenderlovejhawthorn
andcommitted
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>
1 parent f7ff380 commit e3452cf

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

bootstraptest/test_ractor.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,7 +2213,49 @@ def initialize(a)
22132213

22142214
# fork after creating Ractor
22152215
assert_equal 'ok', %q{
2216+
begin
22162217
Ractor.new { Ractor.receive }
22172218
_, status = Process.waitpid2 fork { }
22182219
status.success? ? "ok" : status
2220+
rescue NotImplementedError
2221+
:ok
2222+
end
2223+
}
2224+
2225+
# Ractors should be terminated after fork
2226+
assert_equal 'ok', %q{
2227+
begin
2228+
r = Ractor.new { Ractor.receive }
2229+
_, status = Process.waitpid2 fork {
2230+
begin
2231+
r.take
2232+
raise "ng"
2233+
rescue Ractor::ClosedError
2234+
end
2235+
}
2236+
r.send(123)
2237+
raise unless r.take == 123
2238+
status.success? ? "ok" : status
2239+
rescue NotImplementedError
2240+
:ok
2241+
end
2242+
}
2243+
2244+
# Ractors should be terminated after fork
2245+
assert_equal 'ok', %q{
2246+
begin
2247+
r = Ractor.new { Ractor.receive }
2248+
_, status = Process.waitpid2 fork {
2249+
begin
2250+
r.send(123)
2251+
raise "ng"
2252+
rescue Ractor::ClosedError
2253+
end
2254+
}
2255+
r.send(123)
2256+
raise unless r.take == 123
2257+
status.success? ? "ok" : status
2258+
rescue NotImplementedError
2259+
:ok
2260+
end
22192261
}

ractor.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,6 +2096,9 @@ rb_ractor_terminate_atfork(rb_vm_t *vm, rb_ractor_t *r)
20962096
rb_gc_ractor_cache_free(r->newobj_cache);
20972097
r->newobj_cache = NULL;
20982098
r->status_ = ractor_terminated;
2099+
r->sync.outgoing_port_closed = true;
2100+
r->sync.incoming_port_closed = true;
2101+
r->sync.will_basket.type.e = basket_type_none;
20992102
}
21002103
#endif
21012104

0 commit comments

Comments
 (0)