Lingua's use of Kotlin coroutines causes leaks in web applications
See original GitHub issueI’m using lingua 1.1.0 in a Java web application for language detection. The application is set up to load all models on the first request:
LanguageDetectorBuilder.fromAllLanguages().withPreloadedLanguageModels().build();
When I undeploy the web application from the application server, the models stay in memory. I took a heap dump using Eclipse Memory Anaylzer. The dump shows that there are still instances of the classes related to coroutines (e.g. kotlinx.coroutines.scheduling.CoroutineScheduler$WorkerState
, kotlinx.coroutines.scheduling.WorkQueue
, kotlinx.coroutines.scheduling.CoroutineScheduler
) after undeploying the application. The coroutines still seem to reference the models.
I’ve built a reproducer using only Servlet API that seems to show similar behaviour on Tomcat. Tomcat shows warnings like this:
WARNUNG: The web application [lingua-reproducer] appears to have started a thread named [DefaultDispatcher-worker-9] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: sun.misc.Unsafe.park(Native Method) java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:338) kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.park(CoroutineScheduler.kt:795) kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.tryPark(CoroutineScheduler.kt:740) kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:711) kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
Is there are way to ensure that the threads created by Lingua terminate when the application is undeployed?
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (6 by maintainers)
Thank you.
The problem still seems to exist. I will open a new issue and link a repository with an example application that exhibits the problem.
That’s great. I’m still willing to find a solution to your problem and I’m sure that I will find one with the help of an example application of yours. 😃