1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[Bug #18117] Fix Ractor race condition with GC

rb_objspace_reachable_objects_from requires that the GC not be active.
Since the Ractor barrier is not executed for incremental sweeping,
Ractor may call rb_objspace_reachable_objects_from after sweeping
has started to share objects. This causes a crash that looks like
the following:

```
<internal:ractor>:627: [BUG] rb_objspace_reachable_objects_from() is not supported while during_gc == true
```

Co-authored-by: Vinicius Stock <vinicius.stock@shopify.com>
This commit is contained in:
Peter Zhu 2021-08-17 09:38:40 -04:00
parent b62ed309f0
commit bbedd29b6e
Notes: git 2021-08-24 22:48:08 +09:00
2 changed files with 25 additions and 2 deletions

View file

@ -1418,4 +1418,19 @@ assert_equal "ok", %q{
'ok'
}
# Can yield back values while GC is sweeping [Bug #18117]
assert_equal "ok", %q{
workers = (0...8).map do
Ractor.new do
loop do
10_000.times.map { Object.new }
Ractor.yield Time.now
end
end
end
1_000.times { idle_worker, tmp_reporter = Ractor.select(*workers) }
"ok"
}
end # if !ENV['GITHUB_WORKFLOW']

View file

@ -2368,7 +2368,11 @@ obj_traverse_i(VALUE obj, struct obj_traverse_data *data)
.stop = false,
.data = data,
};
RB_VM_LOCK_ENTER_NO_BARRIER();
{
rb_objspace_reachable_objects_from(obj, obj_traverse_reachable_i, &d);
}
RB_VM_LOCK_LEAVE_NO_BARRIER();
if (d.stop) return 1;
}
break;
@ -2678,7 +2682,11 @@ static int
obj_refer_only_shareables_p(VALUE obj)
{
int cnt = 0;
RB_VM_LOCK_ENTER_NO_BARRIER();
{
rb_objspace_reachable_objects_from(obj, obj_refer_only_shareables_p_i, &cnt);
}
RB_VM_LOCK_LEAVE_NO_BARRIER();
return cnt == 0;
}