Skip to content

Commit ead8f69

Browse files
ZengWen-DTsteipete
andauthored
fix(matrix): preserve redirects when body cancellation fails (#111105)
* fix(matrix): observe redirect body cancellation failures * test(matrix): verify rejection listener cleanup --------- Co-authored-by: ZengWen-DT <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent feb6da2 commit ead8f69

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎extensions/matrix/src/matrix/sdk/transport.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,45 @@ describe("createMatrixGuardedFetch", () => {
477477
clearTestUndiciRuntimeDepsOverride();
478478
});
479479

480+
it("preserves redirect success when the discarded body fails to cancel", async () => {
481+
const unhandledRejections: unknown[] = [];
482+
const onUnhandledRejection = (reason: unknown) => {
483+
unhandledRejections.push(reason);
484+
};
485+
const cancel = vi.fn(() => {
486+
throw new Error("cancel failed");
487+
});
488+
const runtimeFetch = vi
489+
.fn()
490+
.mockResolvedValueOnce(
491+
new Response(new ReadableStream<Uint8Array>({ cancel }), {
492+
status: 302,
493+
headers: { location: "/final" },
494+
}),
495+
)
496+
.mockResolvedValueOnce(new Response("{}", { status: 200 }));
497+
stubRuntimeFetch(runtimeFetch);
498+
process.on("unhandledRejection", onUnhandledRejection);
499+
500+
try {
501+
const guardedFetch = createMatrixGuardedFetch({
502+
ssrfPolicy: { allowPrivateNetwork: true },
503+
});
504+
const response = await guardedFetch("http://127.0.0.1:8008/start");
505+
506+
await expect(response.json()).resolves.toEqual({});
507+
expect(runtimeFetch).toHaveBeenCalledTimes(2);
508+
expect(cancel).toHaveBeenCalledOnce();
509+
await new Promise<void>((resolve) => {
510+
setImmediate(resolve);
511+
});
512+
expect(unhandledRejections).toStrictEqual([]);
513+
} finally {
514+
process.off("unhandledRejection", onUnhandledRejection);
515+
expect(process.listeners("unhandledRejection")).not.toContain(onUnhandledRejection);
516+
}
517+
});
518+
480519
it("rejects and cancels SDK responses above the declared size limit", async () => {
481520
const cancel = vi.fn();
482521
const stream = new ReadableStream<Uint8Array>({ cancel });

‎extensions/matrix/src/matrix/sdk/transport.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ async function fetchWithMatrixGuardedRedirects(params: {
250250
headers.delete("content-length");
251251
}
252252

253-
void response.body?.cancel();
253+
void response.body?.cancel().catch(() => undefined);
254254
await closeDispatcher(dispatcher);
255255
currentUrl = nextUrl;
256256
} catch (error) {

0 commit comments

Comments
 (0)