https://kotlinlang.org logo
#coroutines
Title
# coroutines
p

phldavies

08/17/2020, 9:11 AM
Are there any examples/patterns for graceful shutdown with coroutines?
g

gildor

08/17/2020, 9:16 AM
something like combination of withTimeout and someJob.cancelAndJoin()?
Job.cancel already kinda graceful shutdown, it’s up to coroutine follow or not cancellation, because any suspend call can be wrapped with NonCancellable context
p

phldavies

08/17/2020, 10:18 AM
Thee context of this question is I have started implementing a TCP server using coroutines. I have a supervisorScope wrapping the main receive loop that launches child jobs for each connection. I would like to handle using a Runtime shutdown hook (or some other way of handling SIGINT) such that each client connection is closed and any pending work is finished (such as draining buffers on to downstream services). I’ve tried cancelling the supervisor scope on shutdown, but it doesn’t run the code following it to close things down, even if wrapped in NonCancellable
g

gildor

08/17/2020, 12:45 PM
Hard to say without some example, noncancellable definitely works, or launching coroutine on another scope. Or even final + blocking code, depends on case
30 Views