If I do a JVM app, that starts, does some work, an...
# coroutines
s
If I do a JVM app, that starts, does some work, and then shuts down, I've noticed that coroutines will make my app hang, even when they aren't doing anything. Sometimes my apps
main
function will finish, and then the app won't close for 20 seconds - 2 minutes. I've had to resort to the practice of calling
exitProcess(0)
manually. Is there a proper way to resolve this lag time for our app closing?
t
Are you using Coroutines 1.7.x ? On Android I'm chasing some ANR on app shutdown due to
art::ThreadList::WaitForOtherNonDaemonThreadsToExit
and what you describe would be a possible cause so that would limit where I need to search
🙂
s
I'm doing this with plain JVM apps. This is less of an issue on Android, since the OS chooses when to close the app after it's been minimized.
t
Yes but you do not answer the question 🙂 The ANR I have is that the OS send the stop signal and wait for threads to ends and seems in some cases they do not and now that you posted this those ANR have started when I updated to Coroutines 1.7 so wondering if this is a proper starting point.
s
I've been encountering this for ~ a year. I'm on coroutines version
1.6.4
t
Ok thanks 😞 So no not a clear signal that my issue is coroutines caused 😞 Even if description matches. Have you opened an issue on their tracker that I can follow ?
s
no. If I don't get a result here, that's the plan
p
May be you are using io dispatcher that is backed by cached thread pool that keeps threads for some time after their usage...
You can make thread dump in 'hang' period to check for non daemon threads.
z
Do you actually have coroutines that are still alive? Or are the dispatcher pools just not shutting down?
s
@Zach Klippenstein (he/him) [MOD] All coroutines are done I think
my assumption is that there are non-daemon threads just keeping the application running
Not sure what the proper way to handle this is
d
I haven't seen any reports of similar issues, so it's probably not something fundamental on our side. In any case, I agree with the recommendation to make a thread dump and look at what each existing thread does exactly. Or a coroutine dump: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug/-debug-probes/dump-coroutines.html