# ktor

David Glasser

09/04/2019, 5:12 PM
How does Ktor server (specifically Netty, but in general) behave when the client disconnects, either while still reading the request from it or when writing a response back? Is this something that we have to or can handle? Does the request get cancelled? Is this documented anywhere?


09/05/2019, 9:16 AM
A TCP connection loss could be only detected when an attempt to write is made (sometimes read attempt is also shows up an error but it is not guaranteed). So when a request handling is already in progress, it could get cancelled by the server when the connection loss is detected. It is cancelled via coroutine cancellation mechanism. The other possible consequence is that a
is thrown during response transmission so you can potentially try to catch it. When it is uncaught, the whole request coroutine scope is canceled. So the most reliable I’d say, is to handle cancellation. Notice that sometimes connection loss detection may take long time due to TCP nature.