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

Alexander Romanov

12/18/2018, 4:11 PM
Copy code
val queueListener = launch {
        while (isActive) {
            // do something
        }
    }

    Runtime.getRuntime().addShutdownHook(Thread {
        <http://logger.info|logger.info> { "Cancellation requested" }
        queueListener.cancel()
    })

    queueListener.join()
    <http://logger.info|logger.info> { "Exiting program" }
why would “Exiting program” never show up? are there any other way to do it? I am also considering channel that sends some cancellation event. But what wold be the better way to handle this?
s

streetsofboston

12/18/2018, 4:16 PM
What happens if you add a
yield()
statement inside your
while(isActive)
loop…
a

Alexander Romanov

12/19/2018, 7:39 AM
it doesn’t change anything
s

streetsofboston

12/19/2018, 1:00 PM
Try this for debugging: First: Does your program end or does it hang? If it ends, try this: Instead of a shutdown-hook, use a timer to cancel queueListener. If that works, maybe the 'join' call doesn't work when the program is shut down.
a

Alexander Romanov

12/20/2018, 12:36 PM
I fixed the issue by waiting in thread created by webhook until queueListener reports successful cleanup
4 Views