Concurrent accessing allJars causes the plugin to fail to load (#5158)
Another thread could get the list which is still in the initialization stage, due to first element added. So we need a local variable to avoid this. Also, notice this happens only in concurrency class loader env.
This commit is contained in:
parent
1a373dac03
commit
1215a52d09
|
|
@ -174,22 +174,7 @@ public class AgentClassLoader extends ClassLoader {
|
|||
jarScanLock.lock();
|
||||
try {
|
||||
if (allJars == null) {
|
||||
allJars = new LinkedList<>();
|
||||
for (File path : classpath) {
|
||||
if (path.exists() && path.isDirectory()) {
|
||||
String[] jarFileNames = path.list((dir, name) -> name.endsWith(".jar"));
|
||||
for (String fileName : jarFileNames) {
|
||||
try {
|
||||
File file = new File(path, fileName);
|
||||
Jar jar = new Jar(new JarFile(file), file);
|
||||
allJars.add(jar);
|
||||
logger.info("{} loaded.", file.toString());
|
||||
} catch (IOException e) {
|
||||
logger.error(e, "{} jar file can't be resolved", fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
allJars = doGetJars();
|
||||
}
|
||||
} finally {
|
||||
jarScanLock.unlock();
|
||||
|
|
@ -199,6 +184,26 @@ public class AgentClassLoader extends ClassLoader {
|
|||
return allJars;
|
||||
}
|
||||
|
||||
private LinkedList<Jar> doGetJars() {
|
||||
LinkedList<Jar> jars = new LinkedList<>();
|
||||
for (File path : classpath) {
|
||||
if (path.exists() && path.isDirectory()) {
|
||||
String[] jarFileNames = path.list((dir, name) -> name.endsWith(".jar"));
|
||||
for (String fileName : jarFileNames) {
|
||||
try {
|
||||
File file = new File(path, fileName);
|
||||
Jar jar = new Jar(new JarFile(file), file);
|
||||
jars.add(jar);
|
||||
logger.info("{} loaded.", file.toString());
|
||||
} catch (IOException e) {
|
||||
logger.error(e, "{} jar file can't be resolved", fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return jars;
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor
|
||||
private static class Jar {
|
||||
private final JarFile jarFile;
|
||||
|
|
|
|||
Loading…
Reference in New Issue