How to cancel AsyncClient.read()?
- Dominant language
- Kotlin
- Stars
- 2
- Forks
- 2
- PR merge metrics
- No merged PRs in 30d
Description
In a multiplatform project, I am using the AsyncClient class to communicate with a socket with a proprietary protocol.
I launch two coroutines: one for reading and one for writting. The reading coroutine is a loop similar to this:
```
val asyncClient = AsyncClient.createAndConnect("127.0.0.1", 12345, false)
//...
val readingJob = GlobalScope.launch {
do {
val buffer = ByteArray(1024)
val bytesRead = asyncClient.read(buffer)
//...process buffer
} while(keepReading)
}
```
When I want to finish the communication I don't know exactly how to finish the reading process. When I close the AsyncClient, the AsyncClient.read() function returns a "Socket closed" exception that I cannot handle.
```
java.net.SocketException: Socket closed
at java.base/sun.nio.ch.NioSocketImpl.endRead(NioSocketImpl.java:248)
at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:327)
at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:350)
at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:803)
at java.base/java.net.Socket$SocketInputStream.read(Socket.java:982)
at com.soywiz.korio.net.JvmAsyncClient$read$2$invokeSuspend$$inlined$doIo$1.invokeSuspend(asyncSocketFactoryJvm.kt:77)
(Coroutine boundary)
at com.soywiz.korio.net.JvmAsyncClient$read$2.invokeSuspend(asyncSocketFactoryJvm.kt:77)
at com.soywiz.korio.async.AsyncThread$sync$promise$1.invokeSuspend(AsyncQueue.kt:90)
at com.soywiz.korio.async.AsyncExtKt$_async$1.invokeSuspend(AsyncExt.kt:50)
(Coroutine creation stacktrace)
at kotlinx.coroutines.intrinsics.UndispatchedKt.startCoroutineUndispatched(Undispatched.kt:186)
at kotlinx.coroutines.BuildersKt.async(Unknown Source)
at com.soywiz.korio.async.AsyncExtKt._async(AsyncExt.kt:48)
at com.soywiz.korio.async.AsyncExtKt.asyncImmediately(AsyncExt.kt:14)
at com.soywiz.korio.async.AsyncExtKt.asyncImmediately(AsyncExt.kt:23)
at com.soywiz.korio.async.AsyncThread.sync(AsyncQueue.kt:88)
at com.soywiz.korio.async.AsyncThread.invoke(AsyncQueue.kt:75)
at com.soywiz.korio.net.JvmAsyncClient.read(asyncSocketFactoryJvm.kt:50)
at com.soywiz.korio.stream.AsyncStreamKt.read(AsyncStream.kt:438)
...
```
How can I control this problem? Wich's the correct way to cancel the asyncClient.read() prior to asyncClient.close()?
Thanks so much
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reading AsyncClient.read and AsyncClient.close, then trace the JVM implementation in asyncSocketFactoryJvm.kt and the stream call in AsyncStream.kt. Determine how an in-flight read is expected to end when the socket closes, and whether the existing SocketException behavior is documented or covered by tests. Done means the supported cancellation lifecycle is clear and reproducible.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin
- Domain
- networking
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100