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]>
This commit is contained in:
Theo BrowneandClaude Fable 5 committed 2026-07-28 18:30:25 -07:00
1 parent 95f6b12918
commit a7adbc1161
2 files changed
+23

No files matched your search

@@ -163,4 +163,19 @@ describe("nativeWebSocketConstructor", () => {
expect(closeCalls[0]!.args).toEqual([id, 4000, "bye"]);
expect(socket.readyState).toBe(2);
});
it("ignores a native open that lands after close", async () => {
const socket = await openSocket();
const id = connectionIdOf(socket);
const opened = vi.fn();
socket.addEventListener("open", opened);
// Effect closes while still CONNECTING (open timeout or scope teardown);
// the in-flight native connect can still report success afterwards.
socket.close(1000, "");
mocks.emit("onOpen", { id });
expect(opened).not.toHaveBeenCalled();
expect(socket.readyState).toBe(2);
});
});
+8
View File
@@ -150,6 +150,14 @@ class NativeWebSocket {
}
handleOpen(): void {
// `close()` during CONNECTING (an Effect open-timeout or scope teardown)
// leaves a native connect in flight, so its `didOpen` can still land. Only
// a socket that is still connecting may transition to OPEN; otherwise the
// close would be silently undone and open handlers would fire on a socket
// the caller already gave up on.
if (this.readyState !== NativeWebSocket.CONNECTING) {
return;
}
this.readyState = NativeWebSocket.OPEN;
this.dispatch("open", { type: "open" });
}