Skip to content

Commit 5dce0b3

Browse files
tmmampagent
andauthored
test(zero-swift): close after the WebSocket is up in clientCloseSendsTheCode (#64)
URLSession can report a socket open before it has switched the connection to WebSocket, and a close then ends the TCP connection without a close frame. Network.framework logs "No connected endpoint handler found" in exactly the CI run where the server got no close frame (PR #63's second macOS run), and in no passing one. The test checks that a close sends its code, so the client now reads a message from the server first. DESIGN.md records the race: the server sees an abrupt close instead of the code. Amp-Thread-ID: https://ampcode.com/threads/T-c513a037-a257-45e0-b9a9-15c15edfe859 Co-authored-by: Amp <amp@ampcode.com>
1 parent 099d959 commit 5dce0b3

2 files changed

Lines changed: 4 additions & 2 deletions

File tree

‎swift/DESIGN.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Rationale:
122122
### Transport
123123

124124
- The core defines a transport protocol and never touches a socket directly.
125-
- `URLSessionTransport` implements it with `URLSessionWebSocketTask` and is the default socket on Apple platforms. It accepts frames up to 64 MB (a poke can exceed URLSession's 1 MB default) and reports the server's close code and reason, which the client's error handling reads. Like a browser `WebSocket`, it doesn't follow redirects of the upgrade, so the auth token never goes to another server. Unlike a browser, when the connection ends partway through a frame, URLSession returns the part it has as a whole message. So on its sockets, a message that doesn't make sense holds its error for up to 1 s: when the connection ends without a close frame by then, the client reconnects, as JS does after a browser's abrupt close; otherwise the error stands. Messages that make sense aren't delayed, and other transports keep JS's immediate error.
125+
- `URLSessionTransport` implements it with `URLSessionWebSocketTask` and is the default socket on Apple platforms. It accepts frames up to 64 MB (a poke can exceed URLSession's 1 MB default) and reports the server's close code and reason, which the client's error handling reads. Like a browser `WebSocket`, it doesn't follow redirects of the upgrade, so the auth token never goes to another server. Unlike a browser, when the connection ends partway through a frame, URLSession returns the part it has as a whole message. So on its sockets, a message that doesn't make sense holds its error for up to 1 s: when the connection ends without a close frame by then, the client reconnects, as JS does after a browser's abrupt close; otherwise the error stands. Messages that make sense aren't delayed, and other transports keep JS's immediate error. Also unlike a browser, a close right after the upgrade can end the TCP connection without a close frame: URLSession reports the socket open before it has switched the connection to WebSocket (Network.framework logs `No connected endpoint handler found`). The server then sees an abrupt close instead of the code. Zero closes that early only when the app is hidden or the client closed while connecting, and zero-cache closes the connection the same way either way, only counting it as an `unclean_close` in its WebSocket error metric.
126126
- One writer task per connection sends frames in order.
127127
- Tests use an in-memory transport that replays scenario vectors, and, on Linux end-to-end runs, `NIOTransport` in `Tests/E2ETests` (plain `ws://`). Linux needs it because corelibs Foundation's `URLSessionWebSocketTask` compiles but never completes a connection. On macOS the end-to-end tests use `URLSessionTransport` (`ZERO_E2E_TRANSPORT=nio` switches back).
128128
- Wire details follow `zero-protocol`: connect to `/sync/v53/connect`, put `initConnection` and the auth token in `Sec-WebSocket-Protocol` while they fit under `maxHeaderLength`, and accept binary poke chunks (protocol 52 and later).

‎swift/Tests/E2ETests/TransportProbeTests.swift‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ struct TransportProbeTests {
280280
func clientCloseSendsTheCode(_ kind: TransportKind) async throws {
281281
let got = Box<ScriptedServer.Frame?>(nil)
282282
let server = try await ScriptedServer { c in
283-
await c.write(c.accept())
283+
await c.write(c.accept() + ScriptedServer.text("ready"))
284284
let f = await c.next()
285285
got.withLock { $0 = f }
286286
await c.write(ScriptedServer.close(f?.closeCode ?? 1000))
@@ -289,6 +289,8 @@ struct TransportProbeTests {
289289
}
290290
defer { Task { await server.shutdown() } }
291291
let socket = try await kind.transport.connect(url: server.url, secProtocol: nil)
292+
// A message first: URLSession can return from the upgrade before it has switched the connection to WebSocket, and a close then ends the TCP connection without a close frame (DESIGN.md).
293+
#expect(try await socket.receive() == .text("ready"))
292294
await socket.close(code: 4123, reason: "")
293295
#expect(await server.allClosed(within: .seconds(5)), "the socket stayed open after its close")
294296
// The server's script records the frame a moment after the connection closes.

0 commit comments

Comments
 (0)