Skip to content

Commit

Permalink
Make ClasspathResolver thread-safe
Browse files Browse the repository at this point in the history
We run mustache with huge parallelism in buck2 and as described in https://issuetracker.google.com/issues/137929327 and https://bugs.openjdk.java.net/browse/JDK-8205976 using getResourceAsStream can sometimes be not thread-safe, such as when used with URLClassLoader.

This applies the suggested workaround, which resolves the issue.
  • Loading branch information
ianlevesque authored and spullara committed Apr 24, 2024
1 parent 2342f36 commit a58f7a2
Showing 1 changed file with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import com.github.mustachejava.MustacheResolver;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.*;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -54,14 +50,15 @@ public Reader getReader(String resourceName) {
return null;
}
}
InputStream is = ccl.getResourceAsStream(normalizeResourceName);
if (is == null) {
ClassLoader classLoader = ClasspathResolver.class.getClassLoader();
is = classLoader.getResourceAsStream(normalizeResourceName);
}
else
resource = ClasspathResolver.class.getClassLoader().getResource(normalizeResourceName);

if (is != null) {
return new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
if (resource != null) {
try {
return new BufferedReader(new InputStreamReader(resource.openStream(), StandardCharsets.UTF_8));
} catch (IOException e) {
return null;
}
} else {
return null;
}
Expand Down

3 comments on commit a58f7a2

@panghy
Copy link
Contributor

@panghy panghy commented on a58f7a2 May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are getting com.github.mustachejava.MustacheNotFoundException after pushing 0.9.12 to prod (but strangely unit tests are fine) @ianlevesque @spullara

@spullara
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to reproduce it.

@ianlevesque
Copy link
Contributor Author

@ianlevesque ianlevesque commented on a58f7a2 May 2, 2024 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.