Skip to content

Commit 966fcb7

Browse files
luke-grubernobu
authored andcommitted
lock vm around rb_free_generic_ivar
Currently, this can be reproduced by: r = Ractor.new do a = [1, 2, 3] a.object_id a.dup # this frees the generic ivar for `object_id` on the copied object :done end r.take In debug builds, this hits an assertion failure without this fix.
1 parent 627a5ac commit 966fcb7

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

bootstraptest/test_ractor.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,6 +2319,16 @@ def initialize(a)
23192319
'ok'
23202320
}
23212321

2322+
# take vm lock when deleting generic ivars from the global table
2323+
assert_equal 'ok', %q{
2324+
Ractor.new do
2325+
a = [1, 2, 3]
2326+
a.object_id
2327+
a.dup # this deletes generic ivar on dupped object
2328+
'ok'
2329+
end.take
2330+
}
2331+
23222332
# There are some bugs in Windows with multiple threads in same ractor calling ractor actions
23232333
# Ex: https://github.com/ruby/ruby/actions/runs/14998660285/job/42139383905
23242334
unless /mswin/ =~ RUBY_PLATFORM

variable.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,15 +1273,19 @@ rb_free_generic_ivar(VALUE obj)
12731273

12741274
bool too_complex = rb_shape_obj_too_complex_p(obj);
12751275

1276-
if (st_delete(generic_fields_tbl_no_ractor_check(obj), &key, &value)) {
1277-
struct gen_fields_tbl *fields_tbl = (struct gen_fields_tbl *)value;
1276+
RB_VM_LOCK_ENTER();
1277+
{
1278+
if (st_delete(generic_fields_tbl_no_ractor_check(obj), &key, &value)) {
1279+
struct gen_fields_tbl *fields_tbl = (struct gen_fields_tbl *)value;
12781280

1279-
if (UNLIKELY(too_complex)) {
1280-
st_free_table(fields_tbl->as.complex.table);
1281-
}
1281+
if (UNLIKELY(too_complex)) {
1282+
st_free_table(fields_tbl->as.complex.table);
1283+
}
12821284

1283-
xfree(fields_tbl);
1285+
xfree(fields_tbl);
1286+
}
12841287
}
1288+
RB_VM_LOCK_LEAVE();
12851289
}
12861290

12871291
size_t

0 commit comments

Comments
 (0)