Skip to content

Commit 4976eb3

Browse files
steipetePeter Steinberger
andauthored
fix(ui): honor Gateway readiness abort and subscription ownership (#117048)
* fix(ui): keep canceled gateway waits retired * test(ui): bind Gateway readiness test helper callback * test(ui): isolate server locale preference singleton --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent ae27d79 commit 4976eb3

3 files changed

Lines changed: 156 additions & 6 deletions

File tree

‎test/vitest/vitest.ui-isolated-paths.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Tests in this list depend on module singletons or custom-element registration
33
// matching the current registry, so they need a fresh graph in the isolated lane.
44
export const uiIsolatedTestFiles = [
5+
"ui/src/app/app-host.server-prefs.test.ts",
56
"ui/src/app/bootstrap.test.ts",
67
"ui/src/app/router-outlet.test.ts",
78
"ui/src/components/resizable-divider.test.ts",
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import type { GatewayBrowserClient } from "../api/gateway.ts";
3+
import { waitForGatewayClient } from "./gateway-readiness.ts";
4+
5+
type Gateway = Parameters<typeof waitForGatewayClient>[0];
6+
type GatewaySnapshot = Gateway["snapshot"];
7+
8+
function createGateway(connected = false) {
9+
const client = {} as GatewayBrowserClient;
10+
let snapshot = {
11+
phase: connected ? "connected" : "connecting",
12+
client: connected ? client : null,
13+
} as GatewaySnapshot;
14+
const listeners = new Set<(next: GatewaySnapshot) => void>();
15+
const unsubscribe = vi.fn((listener: (next: GatewaySnapshot) => void) => {
16+
listeners.delete(listener);
17+
});
18+
const subscribe = vi.fn((listener: (next: GatewaySnapshot) => void) => {
19+
listeners.add(listener);
20+
return () => unsubscribe(listener);
21+
});
22+
const gateway: Gateway = {
23+
get snapshot() {
24+
return snapshot;
25+
},
26+
subscribe,
27+
};
28+
return {
29+
gateway,
30+
client,
31+
subscribe,
32+
unsubscribe,
33+
connect: () => {
34+
snapshot = { ...snapshot, phase: "connected", client };
35+
for (const listener of Array.from(listeners)) {
36+
listener(snapshot);
37+
}
38+
},
39+
};
40+
}
41+
42+
describe("waitForGatewayClient", () => {
43+
it("rejects an already-aborted signal before returning a connected client", async () => {
44+
const { gateway, subscribe } = createGateway(true);
45+
const cancellation = new Error("navigation canceled");
46+
const controller = new AbortController();
47+
controller.abort(cancellation);
48+
49+
await expect(waitForGatewayClient(gateway, controller.signal)).rejects.toBe(cancellation);
50+
expect(subscribe).not.toHaveBeenCalled();
51+
});
52+
53+
it("rejects an already-aborted signal before subscribing to a disconnected gateway", async () => {
54+
const { gateway, subscribe } = createGateway();
55+
const controller = new AbortController();
56+
controller.abort();
57+
58+
await expect(waitForGatewayClient(gateway, controller.signal)).rejects.toBe(
59+
controller.signal.reason,
60+
);
61+
expect(subscribe).not.toHaveBeenCalled();
62+
});
63+
64+
it("keeps the existing AbortError contract for a non-error cancellation reason", async () => {
65+
const { gateway } = createGateway(true);
66+
const controller = new AbortController();
67+
controller.abort("navigation canceled");
68+
69+
await expect(waitForGatewayClient(gateway, controller.signal)).rejects.toMatchObject({
70+
name: "AbortError",
71+
message: "Gateway wait aborted",
72+
});
73+
});
74+
75+
it("returns a connected client without subscribing while its signal remains active", async () => {
76+
const { gateway, client, subscribe } = createGateway(true);
77+
78+
await expect(waitForGatewayClient(gateway, new AbortController().signal)).resolves.toBe(client);
79+
expect(subscribe).not.toHaveBeenCalled();
80+
});
81+
82+
it("removes a pending gateway subscription when its signal is aborted", async () => {
83+
const { gateway, unsubscribe } = createGateway();
84+
const cancellation = new Error("navigation canceled");
85+
const controller = new AbortController();
86+
const pending = waitForGatewayClient(gateway, controller.signal);
87+
88+
controller.abort(cancellation);
89+
90+
await expect(pending).rejects.toBe(cancellation);
91+
expect(unsubscribe).toHaveBeenCalledOnce();
92+
});
93+
94+
it("releases a pending subscription once its gateway connects", async () => {
95+
const { gateway, client, connect, unsubscribe } = createGateway();
96+
const controller = new AbortController();
97+
const pending = waitForGatewayClient(gateway, controller.signal);
98+
99+
connect();
100+
101+
await expect(pending).resolves.toBe(client);
102+
expect(unsubscribe).toHaveBeenCalledOnce();
103+
controller.abort();
104+
expect(unsubscribe).toHaveBeenCalledOnce();
105+
});
106+
107+
it("does not retain an abort listener after synchronous connection replay", async () => {
108+
const client = {} as GatewayBrowserClient;
109+
const snapshot = { phase: "connected", client } as GatewaySnapshot;
110+
const unsubscribe = vi.fn();
111+
const gateway: Gateway = {
112+
snapshot: { phase: "connecting", client: null } as GatewaySnapshot,
113+
subscribe(listener) {
114+
listener(snapshot);
115+
return unsubscribe;
116+
},
117+
};
118+
const controller = new AbortController();
119+
const addAbortListener = vi.spyOn(controller.signal, "addEventListener");
120+
const removeAbortListener = vi.spyOn(controller.signal, "removeEventListener");
121+
122+
await expect(waitForGatewayClient(gateway, controller.signal)).resolves.toBe(client);
123+
124+
const abortListener = addAbortListener.mock.calls[0]?.[1];
125+
expect(abortListener).toBeTypeOf("function");
126+
expect(removeAbortListener).toHaveBeenCalledWith("abort", abortListener);
127+
expect(unsubscribe).toHaveBeenCalledOnce();
128+
129+
controller.abort(new Error("late navigation cancellation"));
130+
131+
expect(unsubscribe).toHaveBeenCalledOnce();
132+
});
133+
134+
it("releases a subscription when it synchronously aborts during registration", async () => {
135+
const cancellation = new Error("navigation canceled during registration");
136+
const controller = new AbortController();
137+
const unsubscribe = vi.fn();
138+
const gateway: Gateway = {
139+
snapshot: { phase: "connecting", client: null } as GatewaySnapshot,
140+
subscribe() {
141+
controller.abort(cancellation);
142+
return unsubscribe;
143+
},
144+
};
145+
146+
await expect(waitForGatewayClient(gateway, controller.signal)).rejects.toBe(cancellation);
147+
expect(unsubscribe).toHaveBeenCalledOnce();
148+
});
149+
});

‎ui/src/app/gateway-readiness.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ export function waitForGatewayClient(
1212
signal: AbortSignal,
1313
): Promise<GatewayBrowserClient> {
1414
const current = gateway.snapshot.client;
15+
if (signal.aborted) {
16+
return Promise.reject(abortError(signal));
17+
}
1518
if (current && gateway.snapshot.phase === "connected") {
1619
return Promise.resolve(current);
1720
}
1821
return new Promise((resolve, reject) => {
1922
let unsubscribe: () => void = () => undefined;
20-
let settled = false;
23+
let settled = false; // A synchronous replay must not retain its abort listener.
2124
const cleanup = () => {
2225
unsubscribe();
2326
signal.removeEventListener("abort", onAbort);
@@ -26,19 +29,16 @@ export function waitForGatewayClient(
2629
cleanup();
2730
reject(abortError(signal));
2831
};
32+
signal.addEventListener("abort", onAbort, { once: true });
2933
unsubscribe = gateway.subscribe((snapshot) => {
3034
if (snapshot.phase === "connected" && snapshot.client) {
3135
settled = true;
3236
cleanup();
3337
resolve(snapshot.client);
3438
}
3539
});
36-
if (settled) {
40+
if (settled || signal.aborted) {
3741
unsubscribe();
3842
}
39-
signal.addEventListener("abort", onAbort, { once: true });
40-
if (signal.aborted) {
41-
onAbort();
42-
}
4343
});
4444
}

0 commit comments

Comments
 (0)