Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 171 additions & 0 deletions pkgs/id/web/src/world-rejoin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import { readFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";

/**
* #1461 regression tests: the world page's rejoin path.
*
* A real browser dispatches a socket's `close` event ASYNCHRONOUSLY,
* after the click handler that called `close()` has finished. The fake
* below reproduces that timing - a synchronous dispatch is exactly
* what hid this bug from the existing contract tests.
*/

const here = dirname(fileURLToPath(import.meta.url));

/** The world page markup, read from the Rust template so the test cannot drift from it. */
function worldPageMarkup(): string {
const source = readFileSync(resolve(here, "../../src/web/templates.rs"), "utf8");
const start = source.indexOf("pub fn render_world_page");
const open = source.indexOf('r#"', start);
const close = source.indexOf('"#;', open);
if (start < 0 || open < 0 || close < 0) throw new Error("world page template not found in templates.rs");
return source.slice(open + 3, close);
}

class FakeSocket extends EventTarget {
static readonly CONNECTING = 0;
static readonly OPEN = 1;
static readonly CLOSING = 2;
static readonly CLOSED = 3;
static readonly all: FakeSocket[] = [];

readyState = FakeSocket.CONNECTING;
bufferedAmount = 0;
onclose: ((event: Event) => void) | null = null;
readonly sentFrames: Array<Record<string, unknown>> = [];

constructor(readonly url: string) {
super();
FakeSocket.all.push(this);
}

send(data: string): void {
if (this.readyState !== FakeSocket.OPEN) throw new Error(`send on a socket in state ${this.readyState}`);
this.sentFrames.push(JSON.parse(data) as Record<string, unknown>);
}

/** Client-initiated close: the close EVENT arrives asynchronously, as in a browser. */
close(): void {
if (this.readyState === FakeSocket.CLOSED) return;
this.readyState = FakeSocket.CLOSED;
setTimeout(() => this.dispatchEvent(new Event("close")), 0);
}

/** Server-initiated drop: also asynchronous. */
serverDrop(): void {
this.close();
}

open(): void {
this.readyState = FakeSocket.OPEN;
this.dispatchEvent(new Event("open"));
}

deliver(frame: Record<string, unknown>): void {
this.dispatchEvent(new MessageEvent("message", { data: JSON.stringify(frame) }));
}

sent(type: string): Array<Record<string, unknown>> {
return this.sentFrames.filter((frame) => frame.type === type);
}
}

class FakeResizeObserver {
constructor(readonly callback: ResizeObserverCallback) {}
observe(): void {}
unobserve(): void {}
disconnect(): void {}
}

const flushAsync = () => new Promise((resolveFlush) => setTimeout(resolveFlush, 10));
const sleep = (ms: number) => new Promise((resolveSleep) => setTimeout(resolveSleep, ms));

describe("world page rejoin (ghost WebSocket, #1461)", () => {
const realWebSocket = globalThis.WebSocket;
const realResizeObserver = globalThis.ResizeObserver;
let root: HTMLElement;
const q = <T extends HTMLElement>(selector: string): T => {
const found = root.querySelector<T>(selector);
if (!found) throw new Error(`missing ${selector}`);
return found;
};
const status = () => q("[data-world-status]").textContent;

beforeAll(async () => {
vi.stubGlobal("WebSocket", FakeSocket);
vi.stubGlobal("ResizeObserver", FakeResizeObserver);
sessionStorage.clear();
document.body.innerHTML = worldPageMarkup();
await import("./world");
root = document.querySelector<HTMLElement>("[data-world-app]") as HTMLElement;
expect(root.dataset.initialized).toBe("true");
});

afterAll(() => {
vi.stubGlobal("WebSocket", realWebSocket);
vi.stubGlobal("ResizeObserver", realResizeObserver);
document.body.innerHTML = "";
});

it("rejoin retires the old socket: its async close spawns no ghost connection", async () => {
q<HTMLInputElement>("[data-world-capability]").value = "guest-capability";
q<HTMLButtonElement>("[data-world-join]").click();
expect(FakeSocket.all).toHaveLength(1);
const first = FakeSocket.all[0];
first.open();
expect(first.sent("join")).toHaveLength(1);

// Rejoin. The click retires socket 1, creates socket 2; socket 1's
// close event lands afterwards, asynchronously.
q<HTMLButtonElement>("[data-world-join]").click();
expect(FakeSocket.all).toHaveLength(2);
const second = FakeSocket.all[1];
await flushAsync();

// The retired socket's close must not overwrite the status line...
expect(status()).toBe("Connecting…");
second.open();
// ...and the current socket joins exactly once, on itself.
expect(second.sent("join")).toHaveLength(1);
expect(first.sent("join")).toHaveLength(1);

second.deliver({
type: "snapshot",
snapshot: { world_id: "lobby", current_sequence: 4, participants: [{ id: 1, display_name: "ada" }] },
});
expect(status()).toBe("Connected");

// Frames arriving on the retired socket are ignored, not duplicated.
const log = q("[data-world-log]");
const linesBefore = log.childElementCount;
first.deliver({
type: "event",
event: { sequence: 5, participant_id: 1, kind: { kind: "chat", data: "ghost echo" } },
});
expect(log.childElementCount).toBe(linesBefore);
second.deliver({
type: "event",
event: { sequence: 5, participant_id: 1, kind: { kind: "chat", data: "real line" } },
});
expect(log.childElementCount).toBe(linesBefore + 1);

// Long past the first reconnect backoff (500ms): still exactly two
// sockets created - the ghost would have been a third.
await sleep(700);
expect(FakeSocket.all).toHaveLength(2);
});

it("a genuine unexpected drop still auto-reconnects with backoff", async () => {
const current = FakeSocket.all.at(-1) as FakeSocket;
current.serverDrop();
await flushAsync();
expect(status()).toBe("Disconnected; retrying…");

await vi.waitFor(() => expect(FakeSocket.all).toHaveLength(3), { timeout: 3000, interval: 50 });
const reconnected = FakeSocket.all[2];
reconnected.open();
expect(reconnected.sent("join")).toHaveLength(1);
});
});
42 changes: 33 additions & 9 deletions pkgs/id/web/src/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,37 @@ function initWorld(): void {
}
intentionalClose = false;
const scheme = location.protocol === "https:" ? "wss:" : "ws:";
socket = new WebSocket(`${scheme}//${location.host}/ws/world`);
// #1461: every handler captures THIS connection's socket (`ws`) and
// ignores events once it is no longer the current one. The handlers
// used to read the shared mutable `socket`, so a retired socket's
// async close event (dispatched after a rejoin had already replaced
// it) scheduled a duplicate reconnect, and a stale socket's open
// sent its join frame on the newer socket.
const ws = new WebSocket(`${scheme}//${location.host}/ws/world`);
socket = ws;
setStatus("Connecting…");
socket.addEventListener("open", () => {
ws.addEventListener("open", () => {
if (ws !== socket) return;
reconnects = 0;
socket?.send(JSON.stringify({ type: "join", capability: token, after: cursor || null, world: selectedWorld() }));
ws.send(JSON.stringify({ type: "join", capability: token, after: cursor || null, world: selectedWorld() }));
sendViewport();
});
socket.addEventListener("message", (event) => receive(String(event.data)));
socket.addEventListener("error", () => setStatus("World connection failed", "error"));
socket.addEventListener("close", () => {
ws.addEventListener("message", (event) => {
if (ws !== socket) return;
receive(String(event.data));
});
ws.addEventListener("error", () => {
if (ws !== socket) return;
setStatus("World connection failed", "error");
});
ws.addEventListener("close", () => {
if (ws !== socket) return;
socket = null;
if (intentionalClose) return;
setStatus("Disconnected; retrying…", "error");
const wait = Math.min(500 * 2 ** reconnects++, 10_000);
window.setTimeout(() => {
if (!intentionalClose && capability.value.trim()) connect();
if (!intentionalClose && socket === null && capability.value.trim()) connect();
}, wait);
});
};
Expand Down Expand Up @@ -513,10 +529,18 @@ function initWorld(): void {

joinButton.addEventListener("click", () => {
if (socket) {
// #1461: retire the old socket BEFORE closing it - clear the
// current-socket reference first, so when its close event fires
// (asynchronously, after this handler finishes) the identity
// guard in connect() treats it as stale: no reconnect, no status
// overwrite. The old code set `socket.onclose = null`, which was
// a no-op (connect() registers with addEventListener, never
// onclose), and reset intentionalClose in the same task, before
// the async close event could ever observe it.
intentionalClose = true;
socket.onclose = null;
socket.close();
const retired = socket;
socket = null;
retired.close();
}
intentionalClose = false;
connect();
Expand Down
Loading