Skip to content

Commit e0a7c2b

Browse files
authored
Robustify TCP server (#5968)
1 parent 7bba507 commit e0a7c2b

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

libraries/apollo-mockserver/src/concurrentMain/kotlin/com/apollographql/apollo3/mockserver/TcpServer.concurrent.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.ktor.network.sockets.InetSocketAddress
66
import io.ktor.network.sockets.aSocket
77
import io.ktor.network.sockets.openReadChannel
88
import io.ktor.network.sockets.openWriteChannel
9+
import kotlinx.coroutines.CancellationException
910
import kotlinx.coroutines.CoroutineDispatcher
1011
import kotlinx.coroutines.CoroutineScope
1112
import kotlinx.coroutines.Dispatchers
@@ -41,7 +42,14 @@ class KtorTcpServer(port: Int = 0, private val acceptDelayMillis: Int = 0, dispa
4142
if (acceptDelayMillis > 0) {
4243
delay(acceptDelayMillis.toLong())
4344
}
44-
val socket: WrappedSocket = serverSocket.accept()
45+
val socket: WrappedSocket = try {
46+
serverSocket.accept()
47+
} catch (t: Throwable) {
48+
if (t is CancellationException) { throw t }
49+
delay(1000)
50+
continue
51+
}
52+
4553
val ktorSocket = KtorTcpSocket(socket)
4654
block(ktorSocket)
4755

0 commit comments

Comments
 (0)