question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebSocket throws a vague IOException when internet is disconnected

See original GitHub issue

Ktor Version

1.2.0-rc

Ktor Engine Used(client or server and name)

Client/CIO

JVM Version, Operating System and Relevant Context

OpenJDK 11.0.3, Arch Linux.

Feedback

When the internet is disconnected, a vague java.io.IOException is thrown twice, and it cannot be caught.

Exception in thread "pool-2-thread-3" java.io.IOException: Connection reset by peer
	at java.base/sun.nio.ch.FileDispatcherImpl.read0(Native Method)
	at java.base/sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
	at java.base/sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:276)
	at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:233)
	at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:223)
	at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:358)
	at kotlinx.io.nio.ChannelsKt.read(Channels.kt:117)
	at io.ktor.network.sockets.CIOReaderKt$attachForReadingDirectImpl$1$1.invokeSuspend(CIOReader.kt:75)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.ResumeModeKt.resumeMode(ResumeMode.kt:67)
	at kotlinx.coroutines.DispatchedKt.resume(Dispatched.kt:309)
	at kotlinx.coroutines.DispatchedKt.resumeUnconfined(Dispatched.kt:49)
	at kotlinx.coroutines.DispatchedKt.dispatch(Dispatched.kt:295)
	at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:250)
	at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:260)
	at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:189)
	at io.ktor.network.selector.SelectorManagerSupport.handleSelectedKey(SelectorManagerSupport.kt:83)
	at io.ktor.network.selector.SelectorManagerSupport.handleSelectedKeys(SelectorManagerSupport.kt:63)
	at io.ktor.network.selector.ActorSelectorManager.process(ActorSelectorManager.kt:74)
	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(Dispatched.kt:238)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
brendanwcommented, Dec 18, 2019

Is this exception supposed to be thrown when the internet is disconnected? I am seeing this exception on a benchmarking test when attempting to make 100,000 concurrent requests.

Exception in thread "main" java.net.SocketException: Connection reset by peer
        at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
        at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
        at io.ktor.network.sockets.SocketImpl.connect$ktor_network(SocketImpl.kt:34)
        at io.ktor.network.sockets.SocketImpl$connect$1.invokeSuspend(SocketImpl.kt)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)

https://github.com/brendanw/profilehttp

Are clients expected to handle this error? It would be great to see some guidance from the ktor team.

1reaction
rocketramancommented, Jun 29, 2019

This is not limited to websockets, nor is it limited to a network link disruption. I have this situation for a straight persistent HTTP CIO connection, and it happens when the remote server disconnects the socket. The exception output is distracting and unnecessary, as there is nothing the client code can do about it.

I am using ktor 1.2.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Websockets - onclose - server/client - internet connection ...
I found a solution/workaround for this. Once your internet connection goes down, socket channel remain intact, and client can no more send ...
Read more >
Disable system out for dropped connections : KTOR-753
KTOR-623 WebSocket throws a vague IOException when internet is disconnected ... IOException: Connection reset by peer at sun.nio.ch.
Read more >
Troubleshooting connection issues
You are trying to reach a plain WebSocket server; The server is not reachable; The client is not compatible with the version of...
Read more >
Fix list for IBM WebSphere Application Server V8.5
PI81124, Closing websocket session will throw NullPointerException ... the response from the app server does not indicate ESI caching and client disconnects.
Read more >
A Guide to the Java API for WebSocket
WebSocket provides an alternative to the limitation of efficient ... session) throws IOException { // Get session and WebSocket connection } ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found