Raw client socket crash when connection aborted in Android.
See original GitHub issueKtor Version and Engine Used (client or server and name)
- Ktor version: 1.3.1
- Raw TCP Client
- TLS (optional)
Describe the bug The issue similar to previously reported #1237 When network connection is switched off (for example: airplaine mode) client socket throws an uncatchable exception.
Actual workaround: Using a custom dispatcher with exception handler. Idea comes from this fix: https://github.com/ktorio/ktor/commit/cfd2a841a411e9f4116361e131e0717f38dafe9b
To Reproduce Steps to reproduce the behavior:
- clone https://github.com/fboldog/ktor-rawsocket-crash/
- build and install on android device / emulator
- start the app
- use the CONNECT w/ IO button
- press airplane mode button on quick settings panel
- app instantly crashing
4.1 use CONNECT w/ CUSTOM button 5. press airplane mode button on quick settings panel 6.1 app stays alive
Expected behavior User able to catch connection aborted error and shutdown the flow gracefully
Stack trace
03-17 17:30:28.440 5803-5909/foo.bar.krs E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-6
Process: foo.bar.krs, PID: 5803
java.net.SocketException: shutdown failed: ENOTCONN (Transport endpoint is not connected)
at java.net.PlainSocketImpl.shutdownInput(PlainSocketImpl.java:361)
at java.net.Socket.shutdownInput(Socket.java:635)
at io.ktor.network.sockets.CIOReaderKt$attachForReadingDirectImpl$1.invokeSuspend(CIOReader.kt:111)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:175)
at kotlinx.coroutines.DispatchedTaskKt.resumeUnconfined(DispatchedTask.kt:137)
at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:108)
at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:306)
at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:316)
at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:248)
at io.ktor.network.selector.SelectorManagerSupport.handleSelectedKey(SelectorManagerSupport.kt:84)
at io.ktor.network.selector.SelectorManagerSupport.handleSelectedKeys(SelectorManagerSupport.kt:64)
at io.ktor.network.selector.ActorSelectorManager.process(ActorSelectorManager.kt:73)
at io.ktor.network.selector.ActorSelectorManager$process$1.invokeSuspend(ActorSelectorManager.kt)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
Caused by: android.system.ErrnoException: shutdown failed: ENOTCONN (Transport endpoint is not connected)
at libcore.io.Posix.shutdown(Native Method)
at libcore.io.ForwardingOs.shutdown(ForwardingOs.java:159)
at java.net.PlainSocketImpl.shutdownInput(PlainSocketImpl.java:359)
at java.net.Socket.shutdownInput(Socket.java:635)
at io.ktor.network.sockets.CIOReaderKt$attachForReadingDirectImpl$1.invokeSuspend(CIOReader.kt:111)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:175)
at kotlinx.coroutines.DispatchedTaskKt.resumeUnconfined(DispatchedTask.kt:137)
at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:108)
at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:306)
at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:316)
at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:248)
at io.ktor.network.selector.SelectorManagerSupport.handleSelectedKey(SelectorManagerSupport.kt:84)
at io.ktor.network.selector.SelectorManagerSupport.handleSelectedKeys(SelectorManagerSupport.kt:64)
at io.ktor.network.selector.ActorSelectorManager.process(ActorSelectorManager.kt:73)
at io.ktor.network.selector.ActorSelectorManager$process$1.invokeSuspend(ActorSelectorManager.kt)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665) ```
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Raw client socket crash when connection aborted in Android.
When network connection is switched off (for example: airplaine mode) client socket throws an uncatchable exception. Actual workaround: Using a custom ...
Read more >java.net.SocketException: Connection reset - Stack Overflow
I've a server socket application and fixed a java.net.SocketException: Connection reset case. In my case it happens while reading from a clientSocket Socket ......
Read more >Android crashes with socket error - Visual Studio Feedback
App is opening sockets that are bound to a wifi network adapter (using ConnectivityManager.BindProcessToNetwork). If the app loses the wifi connection (e.g. ...
Read more >Crashes - Android Developers
An Android app crashes whenever there's an unexpected exit caused by an unhandled exception or signal. An app that is written using Java...
Read more >RFC 6455: The WebSocket Protocol
Basically it is intended to be as close to just exposing raw TCP to ... the client MUST _Fail the WebSocket Connection_ and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Works in
1.5.1
I experience the same issue. It seems as if Ktor socket does not propagate up the exception properly, and it ends up unhandled on its own dispatcher.