Is it in any way possible to clean up resources fr...
# coroutines
s
Is it in any way possible to clean up resources from within a coroutine when a SIGINT signal is received (without using a shutdown hook)? I'm trying to do this in a multiplatform project, and it seems like the program is just ignoring anything I put in a
finally
block because coroutines don't block the program from exiting.
I need to run a suspending function that sends a close frame to a server, so my client doesn't just disappear from the server's perspective, but I can't figure out a way to send that close frame without using a shutdown hook.
d
I'm guessing this is on native?
s
Sort of—it’s in common, and I intend to add a native source set in the future. I’m trying to avoid shutdown hooks on recommendation by @olonho, who suggested I have the sockets clean themselves up in a similar manner to MemScope. Unfortunately, as I described, that approach has hit a roadblock when it comes to the JVM.
To be clear, I can’t use shutdown hooks on native, and the JVM isn’t allowing me to send the close frame from within a coroutine (at least not after a SIGINT), so I appear to be stuck.
b
Are you running the coroutine as a
NonCancellable
coroutine?
s
...I completely blanked on the existence of those. I feel stupid lol
Even if I run the entire program within a non-cancellable coroutine, the close frame still doesn't get sent.
d
I take you're not using
GlobalScope
? This seems like weird behaviour.
b
It's unfortunate that the server needs a close frame to understand that the socket is going to be closed, instead of just reacting when the socket is actually closed 😞 Hopefully you figure it out though!
s
It is unfortunate... I may have to resort to making an extra expect class for the socket with different cleanup behavior per platform, which I’d rather not do, but if it’s the only way to solve this problem