8000 Fix a race condition in object_id for shareable objects by casperisfine · Pull Request #13609 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

Fix a race condition in object_id for shareable objects #13609

New issue

Have a question about this project? Sign up for a free GitHub account to open an 8000 issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions ractor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1357,9 +1357,26 @@ make_shareable_check_shareable(VALUE obj)
}
}

if (RB_TYPE_P(obj, T_IMEMO)) {
return traverse_skip;
}
switch (TYPE(obj)) {
case T_IMEMO:
return traverse_skip;
case T_OBJECT:
{
// If a T_OBJECT is shared and has no free capacity, we can't safely store the object_id inline,
// as it would require to move the object content into an external buffer.
// This is only a problem for T_OBJECT, given other types have external fields and can do RCU.
// To avoid this issue, we proactively create the object_id.
shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
attr_index_t capacity = RSHAPE_CAPACITY(shape_id);
attr_index_t free_capacity = capacity - RSHAPE_LEN(shape_id);
if (!rb_shape_has_object_id(shape_id) && capacity && !free_capacity) {
rb_obj_id(obj);
}
}
break;
default:
break;
}

if (!RB_OBJ_FROZEN_RAW(obj)) {
rb_funcall(obj, idFreeze, 0);
Expand Down
77 changes: 77 additions & 0 deletions test/ruby/test_object_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,80 @@ def setup
end
end
end

class TestObjectIdRactor < Test::Unit::TestCase
def test_object_id_race_free
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
begin;
Warning[:experimental] = false
class MyClass
attr_reader :a, :b, :c
def initialize
@a = @b = @c = nil
end
end
N = 10_000
objs = Ractor.make_shareable(N.times.map { MyClass.new })
results = 4.times.map{
Ractor.new(objs) { |objs|
vars = []
ids = []
objs.each do |obj|
vars << obj.a << obj.b << obj.c
ids << obj.object_id
end
[vars, ids]
}
}.map(&:value)
assert_equal 1, results.uniq.size
end;
end

def test_external_object_id_ractor_move
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
begin;
Warning[:experimental] = false
class MyClass
attr_reader :a, :b, :c
def initialize
@a = @b = @c = nil
end
end
obj = Ractor.make_shareable(MyClass.new)
object_id = obj.object_id
obj = Ractor.new { Ractor.receive }.send(obj, move: true).value
assert_equal object_id, obj.object_id
end;
end

def test_object_id_race_free_with_stress_compact
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
begin;
Warning[:experimental] = false
class MyClass
attr_reader :a, :b, :c
def initialize
@a = @b = @c = nil
end
end
N = 20
objs = Ractor.make_shareable(N.times.map { MyClass.new })

GC.stress = true
GC.auto_compact = true if GC.respond_to?(:auto_compact=)

results = 4.times.map{
Ractor.new(objs) { |objs|
vars = []
ids = []
objs.each do |obj|
vars << obj.a << obj.b << obj.c
ids << obj.object_id
end
[vars, ids]
}
}.map(&:value)
assert_equal 1, results.uniq.size
end;
end
end
Loading
0