KT-34604: Fix race condition in KAPT

KAPT was relying on clearing JarFileFactory to make sure
annotation processing does not hold onto annotation processing
classpath once done. Once KAPT switched to using Gradle workers, multiple
KAPT runs were sharing the same class loader ie. the same version
of JarFileFactory. Clearing the cache resulted in race condition,
as some runs were unable to load processors from jars.

This commit fixes this problem by avoiding the use of ServiceLoader
which was causing the issue. Jars would be added to the cache, but
they would never be removed. That's why JarFileFactory had to be
clearned manually. By loading the processor names manually (simply
exploring the classpath), no file handles leak.

Fixes https://youtrack.jetbrains.com/issue/KT-34604
Test: verified against the test project from the bug
This commit is contained in:
Ivan Gavrilovic
2020-06-27 16:28:08 +01:00
committed by Yan Zhulanow
parent 111a2ece72
commit 77ba9a1bbb
2 changed files with 47 additions and 32 deletions
@@ -81,7 +81,7 @@ class ClasspathBasedKapt3Extension(
override fun loadProcessors(): LoadedProcessors {
val efficientProcessorLoader = object : ProcessorLoader(options, logger) {
override fun doLoadProcessors(classLoader: URLClassLoader): List<Processor> {
override fun doLoadProcessors(classpath: LinkedHashSet<File>, classLoader: URLClassLoader): List<Processor> {
return ServiceLoaderLite.loadImplementations(Processor::class.java, classLoader)
}
}