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

merge revision(s) bbedd29b6e: [Backport #18117]

[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>
	---
	 bootstraptest/test_ractor.rb | 15 +++++++++++++++
	 ractor.c                     | 12 ++++++++++--
	 2 files changed, 25 insertions(+), 2 deletions(-)
This commit is contained in:
nagachika 2021-09-05 14:12:20 +09:00
parent 911e75f054
commit 3fb51aec5b
3 changed files with 26 additions and 3 deletions

View file

@ -1379,4 +1379,19 @@ assert_equal "ok", %q{
end end
} }
# 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'] end # if !ENV['GITHUB_WORKFLOW']

View file

@ -2325,7 +2325,11 @@ obj_traverse_i(VALUE obj, struct obj_traverse_data *data)
.stop = false, .stop = false,
.data = data, .data = data,
}; };
rb_objspace_reachable_objects_from(obj, obj_traverse_reachable_i, &d); 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; if (d.stop) return 1;
} }
break; break;
@ -2635,7 +2639,11 @@ static int
obj_refer_only_shareables_p(VALUE obj) obj_refer_only_shareables_p(VALUE obj)
{ {
int cnt = 0; int cnt = 0;
rb_objspace_reachable_objects_from(obj, obj_refer_only_shareables_p_i, &cnt); 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; return cnt == 0;
} }

View file

@ -12,7 +12,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 3 #define RUBY_VERSION_TEENY 3
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
#define RUBY_PATCHLEVEL 127 #define RUBY_PATCHLEVEL 128
#define RUBY_RELEASE_YEAR 2021 #define RUBY_RELEASE_YEAR 2021
#define RUBY_RELEASE_MONTH 9 #define RUBY_RELEASE_MONTH 9