Closed vasilenkoalexey closed 4 years ago
I tried to find a solution
val handler = CoroutineExceptionHandler { _, e ->
println(e)
}
GlobalScope.launch(handler) {
embeddedServer(CIO, 80, parentCoroutineContext = coroutineContext) {
launch {
throw IOException()
}
}.start(wait = false)
}
GlobalScope.launch(handler) {
embeddedServer(CIO, 81, parentCoroutineContext = coroutineContext) {
throw IOException()
}.start(wait = false)
}
GlobalScope.launch(handler) {
embeddedServer(CIO, 82, parentCoroutineContext = coroutineContext) {
CoroutineScope(Dispatchers.Default + environment.parentCoroutineContext).launch {
throw IOException()
}
}.start(wait = false)
}
GlobalScope.launch(handler) {
embeddedServer(CIO, 83, parentCoroutineContext = coroutineContext) {
CoroutineScope(Dispatchers.Default).launch {
throw IOException()
}
}.start(wait = false)
}
All cases were handled by handler
except the last. Exception described above is also not handled. So, looks like some network implementation executed at coroutine scope without parent coroutine context.
How about ideas to add clientContext
parameter to ServerSocketImpl.accept()
, pass it on to SocketImpl
and NIOSocketImpl
where put it to socketContext
?
override val socketContext: CompletableJob = clientContext + Job()
Then HttpServer
should accept input connection somehow like this
val client: Socket = server.accept(coroutineContext)
I think it will allow to handle such exceptions correctly.
It doesn’t seem to be related to the CIO engine only. I’m experiencing the same issue using the Netty engine.
Did you found any workaround in the meantime?
I just turned off keepAlive and exception does not occur
Can't find anything in the docs. How did you achieve that? Can you ignore the keepAlive header from ktor server? How to do it?
Thanks
I turned off it on server side. Just add
intercept(ApplicationCallPipeline.Call) {
call.response.header("Connection", "close")
}
to embeddedServer
Many thanks. It seems to work :)
Closed as resolved
Does not seem to work for ktor 2.0.2. Any idea?
Turning off keepAlive did not solve the problem in my case. Any other work arounds?
Ktor Version
1.2.2
Ktor Engine Used(client or server and name)
CIO embeddedServer
JVM Version, Operating System and Relevant Context
AndroidSDK 28, Android
Feedback
I created a simple embeddedServer at mobile app
and exception occurred during network shutdown (just when turn off Wi-Fi or mobile data). It's as expected but what is the right way to handle it and prevent application crash? Exception only appears if there was at least one request from the client before network shutdown.