Skip to content

Commit 0e3590b

Browse files
committed
test: make DefaultHTTPServer tolerate errors
[skip ci]
1 parent 03843c9 commit 0e3590b

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

TestRunnerTests/Embassy/DefaultHTTPServer.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,31 @@ public final class DefaultHTTPServer: HTTPServer {
8383

8484
// called to handle new connections
8585
private func handleNewConnection() {
86-
let clientSocket = try! acceptSocket.accept()
87-
let (address, port) = try! clientSocket.getPeerName()
86+
// Resilience: a single bad inbound connection must NEVER crash the server.
87+
// This server runs inside the XCUITest runner; a fatal error here aborts
88+
// the entire run ("Executed 0 tests"). A client can reset/close in the
89+
// window between accept() and getPeerName() — common with rapid
90+
// fire-and-forget requests — which surfaces as OSError (EINVAL/ENOTCONN);
91+
// accept() itself can also transiently fail under connection churn. Drop
92+
// the offending connection instead of `try!`-trapping the whole process.
93+
let clientSocket: TCPSocket
94+
do {
95+
clientSocket = try acceptSocket.accept()
96+
} catch {
97+
logger.error("accept() failed; dropping inbound connection: \(error)")
98+
return
99+
}
100+
101+
let address: String
102+
let port: Int
103+
do {
104+
(address, port) = try clientSocket.getPeerName()
105+
} catch {
106+
logger.error("getPeerName() failed; closing inbound connection: \(error)")
107+
clientSocket.close()
108+
return
109+
}
110+
88111
let transport = Transport(socket: clientSocket, eventLoop: eventLoop)
89112
let connection = HTTPConnection(
90113
app: appForConnection,

0 commit comments

Comments
 (0)