Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KotlinSymbolProcessingExtension leaks files due to URLClassLoader not being closed #2159

Closed
pablobaxter opened this issue Oct 23, 2024 · 0 comments · Fixed by #2164
Closed

Comments

@pablobaxter
Copy link
Contributor

pablobaxter commented Oct 23, 2024

} else {
val processingClasspath = options.processingClasspath
val classLoader =
URLClassLoader(processingClasspath.map { it.toURI().toURL() }.toTypedArray(), javaClass.classLoader)
ServiceLoaderLite.loadImplementations(SymbolProcessorProvider::class.java, classLoader).filter {
(options.processors.isEmpty() && it.javaClass.name !in options.excludedProcessors) ||
it.javaClass.name in options.processors
}
}

In the above code for KotlinSymbolProcessingExtension, a URLClassLoader is created but never closed, which leads to a file leak as described here: https://youtrack.jetbrains.com/issue/KT-72172/File-Leak-occurring-in-Kotlin-Daemon

What is happening in this line is that the URLClassLoader holds references to the underlying open files in the URLs, and each time the loadProviders() function is called, it creates a new URLClassLoader, which uses new URL objects, which open new file descriptors. These are closed when URLClassLoader.close() is called, but it never is, and remain open until that class loader is garbage collected.

However, if the class loader is closed once the SymbolProcessorProvider classes are loaded, the compiler will fail later on when it tries to load the classes that extend SymbolProcessor, since the class loader that loaded the provider is now closed. The fix I am working on for the Kotlin compiler (JetBrains/kotlin#5372) is passing in a Disposable and registering a new disposable with the one passed in as the parent, so that it's closed when the compiler is done. However, to fix it in KSP, would require more changes.

I'm unsure if there is a different approach for KSP in closing this class loader, but currently it leads to a situation where the number of open files continues to grow in each new run.

ting-yuan added a commit to ting-yuan/ksp that referenced this issue Oct 23, 2024
ting-yuan added a commit that referenced this issue Oct 23, 2024
github-actions bot pushed a commit that referenced this issue Oct 23, 2024
See also: #2159

(cherry picked from commit c4c9c42)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant