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:
libinglong 2020-07-23 21:51:30 +08:00 committed by GitHub
parent 1a373dac03
commit 1215a52d09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 16 deletions

View File

@ -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;