Reject all pending renderer requests after websocket send failure

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Cursor AgentandTheo Browne committed 2026-02-10 08:25:38 +00:00
1 parent 0705246a51
commit c9c70742f1
2 files changed
+46 -10

No files matched your search

+35
View File
@@ -240,6 +240,41 @@ describe("wsNativeApi", () => {
await expect(secondRequest).resolves.toEqual([]);
});
it("rejects existing pending requests when a later websocket send fails", async () => {
setWindowSearch("?ws=ws%3A%2F%2F127.0.0.1%3A4490");
const { getOrCreateWsNativeApi } = await import("./wsNativeApi");
const api = getOrCreateWsNativeApi();
const firstPending = api.todos.list();
const socket = await waitForSocket();
await waitForCondition(() => socket.sentMessages.length === 1);
MockWebSocket.failSend = true;
const secondPending = api.app.health();
await expect(secondPending).rejects.toThrow(
"Failed to send runtime request 'app.health': mock send failure",
);
await expect(firstPending).rejects.toThrow("websocket errored (mock send failure)");
MockWebSocket.failSend = false;
const recoveryRequest = api.todos.list();
await waitForCondition(() => MockWebSocket.instances.length >= 2);
const recoverySocket = MockWebSocket.instances[1];
await waitForCondition(() => (recoverySocket?.sentMessages.length ?? 0) > 0);
const requestEnvelope = JSON.parse(recoverySocket?.sentMessages[0] ?? "{}") as {
id: string;
};
recoverySocket?.emitMessage(
JSON.stringify({
type: "response",
id: requestEnvelope.id,
ok: true,
result: [],
}),
);
await expect(recoveryRequest).resolves.toEqual([]);
});
it("sends app.health requests to runtime", async () => {
setWindowSearch("?ws=ws%3A%2F%2F127.0.0.1%3A4411");
const { getOrCreateWsNativeApi } = await import("./wsNativeApi");
+11 -10
View File
@@ -125,6 +125,14 @@ class WsNativeApiClient {
constructor(private readonly wsUrl: string) {}
private rejectPendingRequests(errorForRequest: (id: string) => Error) {
for (const [id, pending] of this.pending.entries()) {
clearTimeout(pending.timeout);
pending.reject(errorForRequest(id));
}
this.pending.clear();
}
private connect() {
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
return Promise.resolve(this.socket);
@@ -182,11 +190,7 @@ class WsNativeApiClient {
}
this.socket = null;
for (const [id, pending] of this.pending.entries()) {
clearTimeout(pending.timeout);
pending.reject(requestSocketError(id, event));
}
this.pending.clear();
this.rejectPendingRequests((id) => requestSocketError(id, event));
try {
socket.close();
} catch {
@@ -214,11 +218,7 @@ class WsNativeApiClient {
rejectConnection(runtimeConnectErrorFromClose(event));
return;
}
for (const [id, pending] of this.pending.entries()) {
clearTimeout(pending.timeout);
pending.reject(requestDisconnectError(id, event));
}
this.pending.clear();
this.rejectPendingRequests((id) => requestDisconnectError(id, event));
});
});
@@ -267,6 +267,7 @@ class WsNativeApiClient {
),
);
}
this.rejectPendingRequests((requestId) => requestSocketError(requestId, error));
if (this.socket === socket) {
this.socket = null;
}