Take VM lock in class_switch_superclass (#15356)

Safe multi-ractor subclass list mutation

We need to lock around mutation and accesses of a class's subclasses
list. Unfortunately we also need to do this when creating singleton
classes, as the singleton class does need to go into `super`'s
subclasses list for CC invalidation purposes.
This commit is contained in:
Luke Gruber 2025-12-04 16:10:21 -05:00 committed by GitHub
parent d9aced864b
commit de2c2bd60f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
Notes: git 2025-12-04 21:10:46 +00:00
Merged-By: luke-gru <luke.gru@gmail.com>
2 changed files with 17 additions and 2 deletions

View File

@ -734,8 +734,10 @@ class_detach_subclasses(VALUE klass, VALUE arg)
static void
class_switch_superclass(VALUE super, VALUE klass)
{
class_detach_subclasses(klass, Qnil);
rb_class_subclass_add(super, klass);
RB_VM_LOCKING() {
class_detach_subclasses(klass, Qnil);
rb_class_subclass_add(super, klass);
}
}
/**

View File

@ -917,4 +917,17 @@ CODE
end
end;
end
def test_safe_multi_ractor_subclasses_list_mutation
assert_ractor "#{<<~"begin;"}\n#{<<~'end;'}"
begin;
4.times.map do
Ractor.new do
20_000.times do
Object.new.singleton_class
end
end
end.each(&:join)
end;
end
end