Skip to content

Commit 03b5f37

Browse files
committed
use putIfAbsent to avoid race warnings
1 parent 6296884 commit 03b5f37

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,19 +395,31 @@ public static Source newSource(PythonContext ctxt, String src, String name) {
395395
private final ConcurrentHashMap<Object, Source> cachedSources = new ConcurrentHashMap<>();
396396

397397
public Source newSource(PythonContext ctxt, TruffleFile src, String name) throws IOException {
398-
Source source = cachedSources.get(src);
399-
if (source == null) {
400-
cachedSources.put(src, source = newSource(ctxt, Source.newBuilder(ID, src), name));
398+
try {
399+
return cachedSources.computeIfAbsent(src, t -> {
400+
try {
401+
return newSource(ctxt, Source.newBuilder(ID, src), name);
402+
} catch (IOException e) {
403+
throw new RuntimeException(e);
404+
}
405+
});
406+
} catch (RuntimeException e) {
407+
throw (IOException) e.getCause();
401408
}
402-
return source;
403409
}
404410

405411
public Source newSource(PythonContext ctxt, URL url, String name) throws IOException {
406-
Source source = cachedSources.get(url);
407-
if (source == null) {
408-
cachedSources.put(url, source = newSource(ctxt, Source.newBuilder(ID, url), name));
412+
try {
413+
return cachedSources.computeIfAbsent(url, t -> {
414+
try {
415+
return newSource(ctxt, Source.newBuilder(ID, url), name);
416+
} catch (IOException e) {
417+
throw new RuntimeException(e);
418+
}
419+
});
420+
} catch (RuntimeException e) {
421+
throw (IOException) e.getCause();
409422
}
410-
return source;
411423
}
412424

413425
private static Source newSource(PythonContext ctxt, SourceBuilder srcBuilder, String name) throws IOException {

0 commit comments

Comments
 (0)