Skip to content

Commit e4bea56

Browse files
committed
Lock VM in fork hooks
If we are using multiple Ractors, other Ractors may allocate objects after rb_gc_impl_before_fork is ran because it does not lock the VM. This can cause the GC to be in a bad state since rb_gc_impl_before_fork may have terminated GC threads so a GC cannot run until rb_gc_impl_after_fork is ran.
1 parent fc69d58 commit e4bea56

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

gc/mmtk/mmtk.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ struct objspace {
3838
size_t start_the_world_count;
3939

4040
struct rb_gc_vm_context vm_context;
41+
42+
unsigned int fork_hook_vm_lock_lev;
4143
};
4244

4345
struct MMTk_ractor_cache {
@@ -1045,13 +1047,21 @@ rb_gc_impl_shutdown_call_finalizer(void *objspace_ptr)
10451047
void
10461048
rb_gc_impl_before_fork(void *objspace_ptr)
10471049
{
1050+
struct objspace *objspace = objspace_ptr;
1051+
1052+
objspace->fork_hook_vm_lock_lev = RB_GC_VM_LOCK();
1053+
10481054
mmtk_before_fork();
10491055
}
10501056

10511057
void
10521058
rb_gc_impl_after_fork(void *objspace_ptr, rb_pid_t pid)
10531059
{
1060+
struct objspace *objspace = objspace_ptr;
1061+
10541062
mmtk_after_fork(rb_gc_get_ractor_newobj_cache());
1063+
1064+
RB_GC_VM_UNLOCK(objspace->fork_hook_vm_lock_lev);
10551065
}
10561066

10571067
// Statistics

0 commit comments

Comments
 (0)