arrow icon indicating copy to clipboard operation
arrow copied to clipboard

Leaked `SuspendAppShutdown` / thrown on SIGTERM

Open alexandru opened this issue 10 months ago • 1 comments

I have a Ktor server that, when stopped via SIGTERM, throws an exception:

2026-01-17 18:19:24.273 ERROR [DefaultDispatcher-worker-6] Main - Application failed to start
arrow.continuations.SuspendAppShutdown: SuspendApp shutting down.
	at arrow.continuations.SuspendAppKt$SuspendApp$2$1$result$1$unregister$1$1.invokeSuspend(SuspendApp.kt:37)
	at arrow.continuations.SuspendAppKt$SuspendApp$2$1$result$1$unregister$1$1.invoke(SuspendApp.kt)
	at arrow.continuations.SuspendAppKt$SuspendApp$2$1$result$1$unregister$1$1.invoke(SuspendApp.kt)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndspatched(Undispatched.kt:66)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturnIgnoreTimeout(Undispatched.kt:50)
	at kotlinx.coroutines.TimeoutKt.setupTimeout(Timeout.kt:149)
	at kotlinx.coroutines.TimeoutKt.withTimeout(Timeout.kt:44)
	at kotlinx.coroutines.TimeoutKt.withTimeout-KLykuaI(Timeout.kt:72)
	at arrow.continuations.SuspendAppKt$SuspendApp$2$1$result$1$unregister$1.invokeSuspend(SuspendApp.kt:36)
	at arrow.continuations.SuspendAppKt$SuspendApp$2$1$result$1$unregister$1.invoke(SuspendApp.kt)
	at arrow.continuations.SuspendAppKt$SuspendApp$2$1$result$1$unregister$1.invoke(SuspendApp.kt)
	at arrow.continuations.JvmProcess$onShutdown$shutdown$1.invokeSuspend(Enviroment.jvm.kt:22)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:34)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100)
	at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:263)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:94)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:70)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:48)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at arrow.continuations.JvmProcess.onShutdown$shutdown(Enviroment.jvm.kt:19)
	at java.base/java.lang.Thread.run(Unknown Source)
2026-01-17 18:19:24.279 INFO  [DefaultDispatcher-worker-6] io.ktor.server.Application - prewait delay of 30000ms, turn it off using io.ktor.development=true

I don't know how serious it is, but looking at the definition of that exception, seems to be a CancellationException that should've probably been swallowed.

private class SuspendAppShutdown : CancellationException("SuspendApp shutting down.")

Behaviour seems consistent and has survived refactoring (last time I introduced suspendapp-ktor). But if you want, I can come up with a minimal example.

alexandru avatar Jan 17 '26 18:01 alexandru

I looked into this, and I think the exception comes from the catch block in the app rather than SuspendApp leaking it.

On SIGTERM, SuspendApp cancels the app with SuspendAppShutdown, so awaitCancellation() throws it. SuspendAppShutdown is a CancellationException, which is an Exception, so the block in social-publish at the time (1f7b86e on PR #70) catches and logs it:

try {
    ...
    startServer(...).bind()
    awaitCancellation()
} catch (e: Exception) {
    logger.error(e) { "Application failed to start" }
    throw e
}

Since the catch is inside resourceScope, it runs before the server finalizer. That's why the error shows up before the prewait delay line.

I reproduced it with the same versions (Arrow 2.2.1.1, Ktor 3.3.3, Netty) and got the same stack trace (SuspendApp.kt:37, Enviroment.jvm.kt:22). It goes away if I remove the catch or rethrow CancellationException before logging. I tried that on 2.2.1.1 and 2.3.0.1, with Netty and CIO, SIGTERM and SIGINT, and never saw the error. Exit codes were 143/130 as expected.

social-publish already rethrows it now via rethrowIfFatalOrCancelled, so I think this can be closed. It might still be worth a short note in the SuspendApp docs that shutdown arrives as a CancellationException and should be rethrown from broad catch blocks.

shinseongsu avatar Nov 04 '26 00:11 shinseongsu