Disable GC when building id2ref table

Building that table will likely malloc several time which
can trigger GC and cause race condition by freeing objects
that were just added to the table.

Disabling GC to prevent the race condition isn't elegant,
but iven this is a deprecated callpath that is executed at
most once per process, it seems acceptable.
This commit is contained in:
Jean Boussier 2025-05-15 11:31:11 +02:00
parent 60ffb714d2
commit 31ba881684
Notes: git 2025-05-15 14:29:58 +00:00

9
gc.c
View File

@ -1988,7 +1988,14 @@ object_id_to_ref(void *objspace_ptr, VALUE object_id)
// the table even though it wasn't inserted yet.
id2ref_tbl = st_init_table(&object_id_hash_type);
id2ref_value = TypedData_Wrap_Struct(0, &id2ref_tbl_type, id2ref_tbl);
rb_gc_impl_each_object(objspace, build_id2ref_i, (void *)id2ref_tbl);
// build_id2ref_i will most certainly malloc, which could trigger GC and sweep
// objects we just added to the table.
bool gc_disabled = RTEST(rb_gc_disable_no_rest());
{
rb_gc_impl_each_object(objspace, build_id2ref_i, (void *)id2ref_tbl);
}
if (!gc_disabled) rb_gc_enable();
id2ref_tbl_built = true;
}