ktor websocket client crashes with java.io.IOException: Software caused connection in Android abort
See original GitHub issueVersion: 1.2.2, client, io.ktor:ktor-client-cio, io.ktor:ktor-client-websockets Android 7.0
When using websocket client
To Reproduce code:
val uri = URLBuilder().takeFrom(configuration.url).build()
val httpClientEngine = CIO.create { }
val client = HttpClient(httpClientEngine).config {
install(WebSockets)
}
suspend fun process(session: DefaultClientWebSocketSession): ConnectionStatus {
while (session.isActive && !session.closeReason.isCompleted) {
try {
select<Unit> {
session.incoming.onReceiveOrNull { frame ->
when (frame) {
null -> { // channel has been closed
println("closed")
session.close()
}
else -> Unit
}
}
}
} catch (e: IOException) {
println("io exception: ${e}")
session.close()
} catch (e: Throwable) {
println("exception: ${e}")
session.close()
}
}
}
runBlocking {
try {
when (uri.protocol.isSecure()) {
true -> client.wss(host = uri.host, port = uri.port, path = uri.encodedPath) { process(this) }
false -> client.ws(host = uri.host, port = uri.port, path = uri.encodedPath) { process(this) }
}
} catch (ex: Exception) {
println("catched: ${ex}")
}
}
- Start code with internet enabled and with some websocket url
- disconnect internet IOException will be thrown, but not caught anywhere.
Expected behavior I’m expecting that this IOException won’t leak and can be caught by try/catch.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:16 (3 by maintainers)
Top Results From Across the Web
Ktor-client OkHttp exceptions crash Android app
I have java.net.SocketException: Software caused connection abort in a following scenario: Start streaming with client.get<HttpStatement>(url).
Read more >[Solved]-Ktor Android Client Websocket Connection Failed-kotlin
Coding example for the question Ktor Android Client Websocket Connection Failed-kotlin. ... val client = HttpClient(CIO) { install(WebSockets) } client.
Read more >WhatsNew 1.6 | Ktor Framework
Android : SocketException: Software caused connection abort - for every consecutive call after it failed in the streaming once.
Read more >Software caused connection abort' in java server - Stack ...
Where are you starting the server from? · I'm starting it from an Activity · @UmangMathur Have you try testing with personal wifi...
Read more >How to Fix with java.net.SocketException: Connection reset ...
This occurred when Server closed the connection, while the client is still waiting to read data from its InputStream. For example, if you...
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
Investigation history:
maybe problem is here:
Socket.tls(coroutineContext: CoroutineContext, config: TLSConfig)
function:val reader = openReadChannel()
attachForReading(channel: ByteChannel): WriterJob
CoroutineScope.attachForReadingDirectImpl(...)
// <— this is implemented in internal abstract class NIOSocketImpl<out S> with CoroutineScopewriter(Dispatchers.Unconfined, channel) { .. block .. }
tls()
nor ktor CIO context, this just runs on Dispatchers.Default with Job() So there is no way to catch this exception and its bubbled to global exception handler.We are still waiting while ktor team will fix this critical bug which was introduced in some versions later and never got fixed. This bug cause crashes not only android but also server side thus making multiplatform websockets unusable.
On 2020-01-12, Sun at 21:03, David Alan Cohen notifications@github.com wrote: