Skip to content

Commit 5bb969f

Browse files
ZengWen-DTsteipete
andauthored
fix(tlon): preserve shutdown when body cancellation fails (#111106)
* fix(tlon): observe shutdown body cancellation failures * test(tlon): streamline shutdown cleanup proof * test(tlon): narrow shutdown fixtures --------- Co-authored-by: ZengWen-DT <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent 18b79d9 commit 5bb969f

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎extensions/tlon/src/urbit/sse-client.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,64 @@ describe("UrbitSSEClient", () => {
159159
});
160160
});
161161

162+
describe("close", () => {
163+
it("releases shutdown responses without leaking cancellation failures", async () => {
164+
const unhandledRejections: unknown[] = [];
165+
const onUnhandledRejection = (reason: unknown) => {
166+
unhandledRejections.push(reason);
167+
};
168+
const shutdownResponses = [
169+
{ method: "PUT", label: "unsubscribe" },
170+
{ method: "DELETE", label: "delete" },
171+
].map(({ method, label }) => {
172+
const cancel = vi.fn(() => {
173+
throw new Error(`${label} cancel failed`);
174+
});
175+
const release = vi.fn().mockResolvedValue(undefined);
176+
return {
177+
method,
178+
cancel,
179+
release,
180+
result: {
181+
response: new Response(new ReadableStream<Uint8Array>({ cancel })),
182+
finalUrl: "https://example.com",
183+
release,
184+
},
185+
};
186+
});
187+
const [unsubscribeResponse, deleteResponse] = shutdownResponses;
188+
if (!unsubscribeResponse || !deleteResponse) {
189+
throw new Error("Expected both shutdown response fixtures");
190+
}
191+
const mockUrbitFetch = vi.mocked(urbitFetch);
192+
mockUrbitFetch
193+
.mockResolvedValueOnce(unsubscribeResponse.result)
194+
.mockResolvedValueOnce(deleteResponse.result);
195+
const client = new UrbitSSEClient("https://example.com", "urbauth-~zod=123", {
196+
autoReconnect: false,
197+
});
198+
process.on("unhandledRejection", onUnhandledRejection);
199+
200+
try {
201+
await client.close();
202+
203+
expect(mockUrbitFetch).toHaveBeenCalledTimes(2);
204+
for (const [index, { method, cancel, release }] of shutdownResponses.entries()) {
205+
expect(mockUrbitFetch.mock.calls[index]?.[0].init?.method).toBe(method);
206+
expect(cancel).toHaveBeenCalledOnce();
207+
expect(release).toHaveBeenCalledOnce();
208+
}
209+
await new Promise<void>((resolve) => {
210+
setImmediate(resolve);
211+
});
212+
expect(unhandledRejections).toStrictEqual([]);
213+
} finally {
214+
process.off("unhandledRejection", onUnhandledRejection);
215+
expect(process.listeners("unhandledRejection")).not.toContain(onUnhandledRejection);
216+
}
217+
});
218+
});
219+
162220
describe("reconnection", () => {
163221
it("has autoReconnect enabled by default", () => {
164222
const client = new UrbitSSEClient("https://example.com", "urbauth-~zod=123");

‎extensions/tlon/src/urbit/sse-client.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ export class UrbitSSEClient {
547547
auditContext: "tlon-urbit-unsubscribe",
548548
});
549549
try {
550-
void response.body?.cancel();
550+
void response.body?.cancel().catch(() => undefined);
551551
} finally {
552552
await release();
553553
}
@@ -570,7 +570,7 @@ export class UrbitSSEClient {
570570
auditContext: "tlon-urbit-channel-close",
571571
});
572572
try {
573-
void response.body?.cancel();
573+
void response.body?.cancel().catch(() => undefined);
574574
} finally {
575575
await release();
576576
}

0 commit comments

Comments
 (0)