Skip to content

Commit f7ff380

Browse files
tenderlovejhawthorn
andcommitted
Clean up Ractor cache after fork
Ractors created in a parent process should be properly shut down in the child process. They need their cache cleared and status set to "terminated" Co-authored-by: John Hawthorn <john@hawthorn.email>
1 parent d7ad53f commit f7ff380

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

bootstraptest/test_ractor.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,3 +2210,10 @@ def initialize(a)
22102210
end
22112211
'ok'
22122212
}
2213+
2214+
# fork after creating Ractor
2215+
assert_equal 'ok', %q{
2216+
Ractor.new { Ractor.receive }
2217+
_, status = Process.waitpid2 fork { }
2218+
status.success? ? "ok" : status
2219+
}

ractor.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,6 +2072,8 @@ rb_ractor_main_alloc(void)
20722072
}
20732073

20742074
#if defined(HAVE_WORKING_FORK)
2075+
// Set up the main Ractor for the VM after fork.
2076+
// Puts us in "single Ractor mode"
20752077
void
20762078
rb_ractor_atfork(rb_vm_t *vm, rb_thread_t *th)
20772079
{
@@ -2087,6 +2089,14 @@ rb_ractor_atfork(rb_vm_t *vm, rb_thread_t *th)
20872089
VM_ASSERT(vm->ractor.blocking_cnt == 0);
20882090
VM_ASSERT(vm->ractor.cnt == 1);
20892091
}
2092+
2093+
void
2094+
rb_ractor_terminate_atfork(rb_vm_t *vm, rb_ractor_t *r)
2095+
{
2096+
rb_gc_ractor_cache_free(r->newobj_cache);
2097+
r->newobj_cache = NULL;
2098+
r->status_ = ractor_terminated;
2099+
}
20902100
#endif
20912101

20922102
void rb_thread_sched_init(struct rb_thread_sched *, bool atfork);

ractor_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ void rb_ractor_terminate_interrupt_main_thread(rb_ractor_t *r);
223223
void rb_ractor_terminate_all(void);
224224
bool rb_ractor_main_p_(void);
225225
void rb_ractor_atfork(rb_vm_t *vm, rb_thread_t *th);
226+
void rb_ractor_terminate_atfork(rb_vm_t *vm, rb_ractor_t *th);
226227
VALUE rb_ractor_require(VALUE feature);
227228
VALUE rb_ractor_autoload_load(VALUE space, ID id);
228229

thread.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4740,6 +4740,9 @@ rb_thread_atfork_internal(rb_thread_t *th, void (*atfork)(rb_thread_t *, const r
47404740

47414741
// OK. Only this thread accesses:
47424742
ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
4743+
if (r != vm->ractor.main_ractor) {
4744+
rb_ractor_terminate_atfork(vm, r);
4745+
}
47434746
ccan_list_for_each(&r->threads.set, i, lt_node) {
47444747
atfork(i, th);
47454748
}

0 commit comments

Comments
 (0)