Skip to content

Commit a7adbc1

Browse files
t3dotggclaude
andcommitted
fix(mobile): ignore a late iOS websocket open after close
close() during CONNECTING (an Effect open-timeout or scope teardown) leaves the native connect in flight, so its didOpen could still land and flip the socket back to OPEN, firing open handlers on a socket the caller had already given up on. handleOpen now only transitions from CONNECTING. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 95f6b12 commit a7adbc1

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎apps/mobile/src/lib/nativeWebSocket.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,19 @@ describe("nativeWebSocketConstructor", () => {
163163
expect(closeCalls[0]!.args).toEqual([id, 4000, "bye"]);
164164
expect(socket.readyState).toBe(2);
165165
});
166+
167+
it("ignores a native open that lands after close", async () => {
168+
const socket = await openSocket();
169+
const id = connectionIdOf(socket);
170+
const opened = vi.fn();
171+
socket.addEventListener("open", opened);
172+
173+
// Effect closes while still CONNECTING (open timeout or scope teardown);
174+
// the in-flight native connect can still report success afterwards.
175+
socket.close(1000, "");
176+
mocks.emit("onOpen", { id });
177+
178+
expect(opened).not.toHaveBeenCalled();
179+
expect(socket.readyState).toBe(2);
180+
});
166181
});

‎apps/mobile/src/lib/nativeWebSocket.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ class NativeWebSocket {
150150
}
151151

152152
handleOpen(): void {
153+
// `close()` during CONNECTING (an Effect open-timeout or scope teardown)
154+
// leaves a native connect in flight, so its `didOpen` can still land. Only
155+
// a socket that is still connecting may transition to OPEN; otherwise the
156+
// close would be silently undone and open handlers would fire on a socket
157+
// the caller already gave up on.
158+
if (this.readyState !== NativeWebSocket.CONNECTING) {
159+
return;
160+
}
153161
this.readyState = NativeWebSocket.OPEN;
154162
this.dispatch("open", { type: "open" });
155163
}

0 commit comments

Comments
 (0)