https://kotlinlang.org logo
#ktor
Title
# ktor
a

Aljoscha Grebe

12/01/2021, 10:57 PM
Hi, I'm trying to use ktor-network to send and receive some data over UDP. I have however run into a problem which I was able to break down to a few lines of code:
Copy code
suspend fun main() {
    val socket = aSocket(ActorSelectorManager(<http://Dispatchers.IO|Dispatchers.IO>)).udp().connect(InetSocketAddress("localhost", 1111))

    val input = socket.openReadChannel()

    input.readByte() // This line causes the exception
}
This code always throws the following exception:
Copy code
Exception in thread "main" java.lang.IllegalStateException: Handler for READ is already registered
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at io.ktor.utils.io.ExceptionUtilsJvmKt$createConstructor$$inlined$safeCtor$1.invoke(ExceptionUtilsJvm.kt:103)
	at io.ktor.utils.io.ExceptionUtilsJvmKt$createConstructor$$inlined$safeCtor$1.invoke(ExceptionUtilsJvm.kt:90)
	at io.ktor.utils.io.ExceptionUtilsJvmKt.tryCopyException(ExceptionUtilsJvm.kt:66)
	at io.ktor.utils.io.ByteBufferChannelKt.rethrowClosed(ByteBufferChannel.kt:2460)
	at io.ktor.utils.io.ByteBufferChannelKt.access$rethrowClosed(ByteBufferChannel.kt:1)
	at io.ktor.utils.io.ByteBufferChannel.setupStateForRead(ByteBufferChannel.kt:310)
	at io.ktor.utils.io.ByteBufferChannel.readByte(ByteBufferChannel.kt:2540)
	at com.example.TestKt.main(Test.kt:13) // Line with readByte()
	at com.example.TestKt$main$2.invoke(Test.kt)
	at com.example.TestKt$main$2.invoke(Test.kt)
	at kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt$createCoroutineUnintercepted$$inlined$createCoroutineFromSuspendFunction$IntrinsicsKt__IntrinsicsJvmKt$1.invokeSuspend(IntrinsicsJvm.kt:205)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlin.coroutines.ContinuationKt.startCoroutine(Continuation.kt:115)
	at kotlin.coroutines.jvm.internal.RunSuspendKt.runSuspend(RunSuspend.kt:19)
	at com.example.TestKt.main(Test.kt)
Caused by: java.lang.IllegalStateException: Handler for READ is already registered
	at io.ktor.network.selector.InterestSuspensionsMap.addSuspension(InterestSuspensionsMap.kt:34)
	at io.ktor.network.selector.SelectorManagerSupport.select(SelectorManagerSupport.kt:40)
	at io.ktor.network.sockets.CIOReaderKt$attachForReadingImpl$1.invokeSuspend(CIOReader.kt:46)
	at io.ktor.network.sockets.CIOReaderKt$attachForReadingImpl$1.invoke(CIOReader.kt)
	at io.ktor.network.sockets.CIOReaderKt$attachForReadingImpl$1.invoke(CIOReader.kt)
	at io.ktor.utils.io.CoroutinesKt$launchChannel$job$1.invokeSuspend(Coroutines.kt:132)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:367)
	at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.startCoroutineImpl(Builders.common.kt:192)
	at kotlinx.coroutines.BuildersKt.startCoroutineImpl(Unknown Source)
	at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:134)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
	at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
	at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
	at io.ktor.utils.io.CoroutinesKt.launchChannel(Coroutines.kt:123)
	at io.ktor.utils.io.CoroutinesKt.writer(Coroutines.kt:77)
	at io.ktor.network.sockets.CIOReaderKt.attachForReadingImpl(CIOReader.kt:27)
	at io.ktor.network.sockets.NIOSocketImpl$attachForReading$1.invoke(NIOSocketImpl.kt:43)
	at io.ktor.network.sockets.NIOSocketImpl$attachForReading$1.invoke(NIOSocketImpl.kt:41)
	at io.ktor.network.sockets.NIOSocketImpl.attachFor(NIOSocketImpl.kt:80)
	at io.ktor.network.sockets.NIOSocketImpl.attachForReading(NIOSocketImpl.kt:41)
	at io.ktor.network.sockets.SocketsKt.openReadChannel(Sockets.kt:108)
	at com.example.TestKt.main(Test.kt:11) // Line with .openReadChannel()
	... 7 more
Edit: The only dependency I have n the example project is ktor-network:1.6.6
a

Aleksei Tirman [JB]

12/02/2021, 7:18 AM
I've created an issue to address this problem.
a

Aljoscha Grebe

12/02/2021, 10:53 AM
Thanks! I wasn't sure if this really was an issue with Ktor or if I was just fundamentally missing something
61 Views