Skip to content

Commit 16a1cc4

Browse files
committed
Fix exceptions ignored in select code
1 parent 5af7ee9 commit 16a1cc4

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

ktor-network/jvm/src/io/ktor/network/sockets/DatagramSocketImpl.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import io.ktor.network.util.*
99
import io.ktor.utils.io.core.*
1010
import kotlinx.coroutines.*
1111
import kotlinx.coroutines.channels.*
12-
import kotlinx.io.IOException
1312
import java.nio.*
1413
import java.nio.channels.*
1514

@@ -52,7 +51,6 @@ internal class DatagramSocketImpl(
5251
channel.send(receiveImpl())
5352
}
5453
} catch (_: ClosedChannelException) {
55-
} catch (cause: IOException) {
5654
}
5755
}
5856

ktor-network/jvmAndPosix/test/io/ktor/network/sockets/tests/UDPSocketTest.kt

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,28 @@ class UDPSocketTest {
263263
.udp()
264264
.bind()
265265

266-
val remoteAddress = InetSocketAddress("127.0.0.1", (server.localAddress as InetSocketAddress).port)
267-
val socket = aSocket(selector).udp().connect(remoteAddress)
266+
server.use {
267+
val remoteAddress = InetSocketAddress("127.0.0.1", (server.localAddress as InetSocketAddress).port)
268+
val socket = aSocket(selector).udp().connect(remoteAddress)
268269

269-
socket.send(Datagram(buildPacket { writeText("hello") }, remoteAddress))
270-
assertEquals("hello", server.receive().packet.readText())
270+
socket.send(Datagram(buildPacket { writeText("hello") }, remoteAddress))
271+
assertEquals("hello", server.receive().packet.readText())
272+
}
273+
}
274+
275+
@Test
276+
fun testReceiveError() = testSockets { selector ->
277+
val socket = aSocket(selector)
278+
.udp()
279+
.bind()
280+
281+
socket.use {
282+
selector.close()
283+
// Receive fails due to closed selector exception
284+
assertFails {
285+
socket.receive()
286+
}
287+
}
271288
}
272289
}
273290

ktor-network/posix/src/io/ktor/network/sockets/CIOReader.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import io.ktor.utils.io.*
1010
import io.ktor.utils.io.errors.*
1111
import kotlinx.cinterop.*
1212
import kotlinx.coroutines.*
13-
import kotlinx.io.IOException
1413

1514
@OptIn(ExperimentalForeignApi::class)
1615
internal fun CoroutineScope.attachForReadingImpl(
@@ -49,11 +48,7 @@ internal fun CoroutineScope.attachForReadingImpl(
4948
}
5049

5150
if (count == 0) {
52-
try {
53-
selector.select(selectable, SelectInterest.READ)
54-
} catch (_: IOException) {
55-
break
56-
}
51+
selector.select(selectable, SelectInterest.READ)
5752
}
5853
}
5954

ktor-network/posix/src/io/ktor/network/sockets/DatagramSocketNative.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ internal class DatagramSocketNative(
4343
val received = readDatagram()
4444
channel.send(received)
4545
}
46-
} catch (_: ClosedSendChannelException) {
47-
} catch (cause: IOException) {
48-
} catch (cause: PosixException) {
46+
} catch (_: ClosedSocketException) {
4947
}
5048
}
5149

@@ -88,7 +86,7 @@ internal class DatagramSocketNative(
8886
}
8987

9088
when (bytesRead) {
91-
0L -> throw IOException("Failed reading from closed socket")
89+
0L -> throw ClosedSocketException()
9290
-1L -> {
9391
val error = getSocketError()
9492
if (isWouldBlockError(error)) return null
@@ -106,3 +104,5 @@ internal class DatagramSocketNative(
106104
}
107105
}
108106
}
107+
108+
private class ClosedSocketException : IOException()

0 commit comments

Comments
 (0)