question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lingua's use of Kotlin coroutines causes leaks in web applications

See original GitHub issue

I’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:closed
  • Created 2 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dnb-erik-brangscommented, Nov 29, 2021

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.

0reactions
pemistahlcommented, Nov 29, 2021

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. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Memory leak in coroutines 1.4.x · Issue #2564 - GitHub
I've encountered a memory leak which I believe is caused by a race condition in the coroutine library. I've simplified the leak down...
Read more >
Unavoidable memory leak when using coroutines - Android
It appears that coroutines retain references that prevent garbage collection even in trivial examples.
Read more >
Kotlin coroutines on Android
A coroutine is a concurrency design pattern that you can use on Android to simplify code that executes asynchronously.
Read more >
Kotlin Coroutines — What, Why & How? | by Saurabh Pant
As the concurrency is propagated through the hierarchy, lesser are the chances of leak. Code in synchronous fashion. Launching and running a ...
Read more >
Kotlin Coroutines: make it make sense - Educative.io
On second thought, it's a terrible idea because there is a high chance that there will be memory leaks with this approach. To...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found