Skip to content

Commit 1d9f78c

Browse files
committed
Fix test_webSocket hang (swiftlang#4973)
1 parent 635aecd commit 1d9f78c

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Sources/FoundationNetworking/URLSession/URLSessionTask.swift

+6
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,12 @@ open class URLSessionWebSocketTask : URLSessionTask {
849849
}
850850
}
851851
self.receiveCompletionHandlers.removeAll()
852+
for handler in self.pongCompletionHandlers {
853+
session.delegateQueue.addOperation {
854+
handler(taskError)
855+
}
856+
}
857+
self.pongCompletionHandlers.removeAll()
852858
self._getProtocol { urlProtocol in
853859
self.workQueue.async {
854860
if self.handshakeCompleted && self.state != .completed {

Tests/Foundation/Tests/TestURLSession.swift

+9-2
Original file line numberDiff line numberDiff line change
@@ -1893,8 +1893,15 @@ class TestURLSession: LoopbackServerTest {
18931893
XCTFail("Unexpected Data Message")
18941894
}
18951895

1896-
try await task.sendPing()
1897-
1896+
do {
1897+
try await task.sendPing()
1898+
// Server hasn't closed the connection yet
1899+
} catch {
1900+
// Server closed the connection before we could process the pong
1901+
let urlError = try XCTUnwrap(error as? URLError)
1902+
XCTAssertEqual(urlError._nsError.code, NSURLErrorNetworkConnectionLost)
1903+
}
1904+
18981905
wait(for: [delegate.expectation], timeout: 50)
18991906

19001907
do {

0 commit comments

Comments
 (0)