Skip to content

Commit 7c35d85

Browse files
luke-gruberetiennebarrie
authored andcommitted
Hook up autoload method to use rb_ractor_autoload_load when in ractor
The following code had issues because `autoload` was not calling into the appropriate ractor autoload code: ```ruby require "tmpdir" rs = [] tmpdir = Dir.mktmpdir("autoload") path = File.join(tmpdir, "a.rb") File.open(path, 'w') do |f| f.write("class A; end\n") end 100.times do rs << Ractor.new(path) do |path| autoload :A, path end end while rs.any? r, obj = Ractor.select(*rs) rs.delete(r) end ```
1 parent f9b05a4 commit 7c35d85

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

variable.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3046,26 +3046,30 @@ autoload_synchronized(VALUE _arguments)
30463046
void
30473047
rb_autoload_str(VALUE module, ID name, VALUE feature)
30483048
{
3049-
const rb_namespace_t *ns = rb_current_namespace();
3050-
VALUE current_namespace = rb_get_namespace_object((rb_namespace_t *)ns);
3049+
VALUE result;
30513050

30523051
if (!rb_is_const_id(name)) {
30533052
rb_raise(rb_eNameError, "autoload must be constant name: %"PRIsVALUE"", QUOTE_ID(name));
30543053
}
3055-
30563054
Check_Type(feature, T_STRING);
30573055
if (!RSTRING_LEN(feature)) {
30583056
rb_raise(rb_eArgError, "empty feature name");
30593057
}
30603058

3061-
struct autoload_arguments arguments = {
3062-
.module = module,
3063-
.name = name,
3064-
.feature = feature,
3065-
.namespace = current_namespace,
3066-
};
3059+
if (rb_ractor_main_p()) {
3060+
const rb_namespace_t *ns = rb_current_namespace();
3061+
VALUE current_namespace = rb_get_namespace_object((rb_namespace_t *)ns);
3062+
struct autoload_arguments arguments = {
3063+
.module = module,
3064+
.name = name,
3065+
.feature = feature,
3066+
.namespace = current_namespace,
3067+
};
30673068

3068-
VALUE result = rb_mutex_synchronize(autoload_mutex, autoload_synchronized, (VALUE)&arguments);
3069+
result = rb_mutex_synchronize(autoload_mutex, autoload_synchronized, (VALUE)&arguments);
3070+
} else {
3071+
result = rb_ractor_autoload_load(module, name);
3072+
}
30693073

30703074
if (result == Qtrue) {
30713075
const_added(module, name);
@@ -3449,7 +3453,6 @@ rb_autoload_load(VALUE module, ID name)
34493453
return Qfalse;
34503454
}
34513455

3452-
// At this point, we assume there might be autoloading, so fail if it's ractor:
34533456
if (UNLIKELY(!rb_ractor_main_p())) {
34543457
return rb_ractor_autoload_load(module, name);
34553458
}

0 commit comments

Comments
 (0)