Skip to content

Fix exceptions ignored in select code #4681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import io.ktor.network.util.*
import io.ktor.utils.io.core.*
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.io.IOException
import java.nio.*
import java.nio.channels.*

Expand Down Expand Up @@ -52,7 +51,6 @@ internal class DatagramSocketImpl(
channel.send(receiveImpl())
}
} catch (_: ClosedChannelException) {
} catch (cause: IOException) {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,28 @@ class UDPSocketTest {
.udp()
.bind()

val remoteAddress = InetSocketAddress("127.0.0.1", (server.localAddress as InetSocketAddress).port)
val socket = aSocket(selector).udp().connect(remoteAddress)
server.use {
val remoteAddress = InetSocketAddress("127.0.0.1", (server.localAddress as InetSocketAddress).port)
val socket = aSocket(selector).udp().connect(remoteAddress)

socket.send(Datagram(buildPacket { writeText("hello") }, remoteAddress))
assertEquals("hello", server.receive().packet.readText())
socket.send(Datagram(buildPacket { writeText("hello") }, remoteAddress))
assertEquals("hello", server.receive().packet.readText())
}
}

@Test
fun testReceiveError() = testSockets { selector ->
val socket = aSocket(selector)
.udp()
.bind()

socket.use {
selector.close()
// Receive fails due to closed selector exception
assertFails {
socket.receive()
}
}
}
}

Expand Down
7 changes: 1 addition & 6 deletions ktor-network/posix/src/io/ktor/network/sockets/CIOReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import io.ktor.utils.io.*
import io.ktor.utils.io.errors.*
import kotlinx.cinterop.*
import kotlinx.coroutines.*
import kotlinx.io.IOException

@OptIn(ExperimentalForeignApi::class)
internal fun CoroutineScope.attachForReadingImpl(
Expand Down Expand Up @@ -49,11 +48,7 @@ internal fun CoroutineScope.attachForReadingImpl(
}

if (count == 0) {
try {
selector.select(selectable, SelectInterest.READ)
} catch (_: IOException) {
break
}
selector.select(selectable, SelectInterest.READ)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ internal class DatagramSocketNative(
val received = readDatagram()
channel.send(received)
}
} catch (_: ClosedSendChannelException) {
} catch (cause: IOException) {
} catch (cause: PosixException) {
} catch (_: ClosedSocketException) {
}
}

Expand Down Expand Up @@ -88,7 +86,7 @@ internal class DatagramSocketNative(
}

when (bytesRead) {
0L -> throw IOException("Failed reading from closed socket")
0L -> throw ClosedSocketException()
-1L -> {
val error = getSocketError()
if (isWouldBlockError(error)) return null
Expand All @@ -106,3 +104,5 @@ internal class DatagramSocketNative(
}
}
}

private class ClosedSocketException : IOException()