Hello guys, I am have a Ktor backend which, among ...
# coroutines
e
Hello guys, I am have a Ktor backend which, among other things, runs a poller.
Copy code
flow {
            while(true){
                delay(interval)
                emit(true)
            }
        }.onEach {
            try {
                //poll here
            } catch (t: Throwable) {
                logger.error(t.message ?: "")
            }
            logger.debug("Poller run finished")
        }.launchIn(application)
Now this can run for weeks without problems, and then all of a sudden the poller stops. I am totally lost as to why that could happen. Does anyone have a clue?
r
could you provide more details about your
application
scope? Does it have a supervisor job, have you attached an exception handler to it etc
I have seen similar issues where, without using a supervisor job, an unrelated task launched in the same scope throws, there is no exception handler to provide a clue, and because the scope is cancelled, seemingly unrelated jobs are also cancelled because the parent was
e
application is the io.ktor.server.application process
r
looks like that does have a SupervisorJob
does the
poll
ever not return? You could use this to cancel an outstanding poll request if it doesn't return before the next interval fires
Copy code
application.launch {
    flow {
        while(true){
            delay(interval)
            emit(true)
        }
    }.collectLatest {
        try {
            //poll here
        } catch (t: Throwable) {
            logger.error(t.message ?: "")
        }
        logger.debug("Poller run finished")
    }
}
other than that, I don't know I'm afraid
or ktor client timeout if that's not set and is being used for the poll
e
Yes thanks a lot! I just was grepping around in the logfiles, and the FTP poll started but never finished after which the poller seemed to stop. Was playing around with wrapping the poll call with
withTimeout
until I saw your elegant solution. Thanks a lot, pretty positive this fixed it.
👍 1