Skip to content

Commit e690de0

Browse files
committed
Test if we should add an autoload constant in the replace loop
* So there is no race between the test and set. (cherry picked from commit 324239a)
1 parent 0d9ca9d commit e690de0

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/main/java/org/truffleruby/core/module/ModuleFields.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ public RubyConstant setConstant(RubyContext context, Node currentNode, String na
343343
@TruffleBoundary
344344
public void setAutoloadConstant(RubyContext context, Node currentNode, String name, RubyString filename) {
345345
RubyConstant autoloadConstant = setConstantInternal(context, currentNode, name, filename, true);
346+
if (autoloadConstant == null) {
347+
return;
348+
}
349+
346350
if (context.getOptions().LOG_AUTOLOAD) {
347351
RubyLanguage.LOGGER.info(() -> String.format(
348352
"%s: setting up autoload %s with %s",
@@ -371,6 +375,10 @@ private RubyConstant setConstantInternal(RubyContext context, Node currentNode,
371375
RubyConstant newConstant;
372376
do {
373377
previous = constants.get(name);
378+
if (autoload && previous != null && previous.hasValue()) {
379+
// abort, do not set an autoload constant, the constant already has a value
380+
return null;
381+
}
374382
newConstant = newConstant(currentNode, name, value, autoload, previous);
375383
} while (!ConcurrentOperations.replace(constants, name, previous, newConstant));
376384

src/main/java/org/truffleruby/core/module/ModuleNodes.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,7 @@ protected Object autoload(RubyModule module, String name, RubyString filename) {
580580
throw new RaiseException(getContext(), coreExceptions().argumentError("empty file name", this));
581581
}
582582

583-
final RubyConstant constant = module.fields.getConstant(name);
584-
if (constant == null || !constant.hasValue()) {
585-
module.fields.setAutoloadConstant(getContext(), this, name, filename);
586-
}
587-
583+
module.fields.setAutoloadConstant(getContext(), this, name, filename);
588584
return nil;
589585
}
590586
}

0 commit comments

Comments
 (0)