File tree 4 files changed +26
-16
lines changed
jvm/src/io/ktor/network/sockets
jvmAndPosix/test/io/ktor/network/sockets/tests
posix/src/io/ktor/network/sockets 4 files changed +26
-16
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,6 @@ import io.ktor.network.util.*
9
9
import io.ktor.utils.io.core.*
10
10
import kotlinx.coroutines.*
11
11
import kotlinx.coroutines.channels.*
12
- import kotlinx.io.IOException
13
12
import java.nio.*
14
13
import java.nio.channels.*
15
14
@@ -52,7 +51,6 @@ internal class DatagramSocketImpl(
52
51
channel.send(receiveImpl())
53
52
}
54
53
} catch (_: ClosedChannelException ) {
55
- } catch (cause: IOException ) {
56
54
}
57
55
}
58
56
Original file line number Diff line number Diff line change @@ -263,11 +263,28 @@ class UDPSocketTest {
263
263
.udp()
264
264
.bind()
265
265
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)
268
269
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
+ }
271
288
}
272
289
}
273
290
Original file line number Diff line number Diff line change @@ -10,7 +10,6 @@ import io.ktor.utils.io.*
10
10
import io.ktor.utils.io.errors.*
11
11
import kotlinx.cinterop.*
12
12
import kotlinx.coroutines.*
13
- import kotlinx.io.IOException
14
13
15
14
@OptIn(ExperimentalForeignApi ::class )
16
15
internal fun CoroutineScope.attachForReadingImpl (
@@ -49,11 +48,7 @@ internal fun CoroutineScope.attachForReadingImpl(
49
48
}
50
49
51
50
if (count == 0 ) {
52
- try {
53
- selector.select(selectable, SelectInterest .READ )
54
- } catch (_: IOException ) {
55
- break
56
- }
51
+ selector.select(selectable, SelectInterest .READ )
57
52
}
58
53
}
59
54
Original file line number Diff line number Diff line change @@ -43,9 +43,7 @@ internal class DatagramSocketNative(
43
43
val received = readDatagram()
44
44
channel.send(received)
45
45
}
46
- } catch (_: ClosedSendChannelException ) {
47
- } catch (cause: IOException ) {
48
- } catch (cause: PosixException ) {
46
+ } catch (_: ClosedSocketException ) {
49
47
}
50
48
}
51
49
@@ -88,7 +86,7 @@ internal class DatagramSocketNative(
88
86
}
89
87
90
88
when (bytesRead) {
91
- 0L -> throw IOException ( " Failed reading from closed socket " )
89
+ 0L -> throw ClosedSocketException ( )
92
90
- 1L -> {
93
91
val error = getSocketError()
94
92
if (isWouldBlockError(error)) return null
@@ -106,3 +104,5 @@ internal class DatagramSocketNative(
106
104
}
107
105
}
108
106
}
107
+
108
+ private class ClosedSocketException : IOException ()
You can’t perform that action at this time.
0 commit comments