Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix adapter logic for aborting requests #10046

Merged
merged 1 commit into from
Oct 2, 2024
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
6 changes: 6 additions & 0 deletions .changeset/silly-gifts-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@remix-run/dev": patch
"@remix-run/express": patch
---

Fix adapter logic for aborting `request.signal` so we don't incorrectly abort on the `close` event for successful requests
12 changes: 8 additions & 4 deletions packages/remix-dev/vite/node-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ export function fromNodeRequest(
);
let url = new URL(nodeReq.originalUrl, origin);

// Abort action/loaders once we can no longer write a response
let controller = new AbortController();
nodeRes.on("close", () => controller.abort());

let controller: AbortController | null = new AbortController();
let init: RequestInit = {
method: nodeReq.method,
headers: fromNodeHeaders(nodeReq.headers),
Expand All @@ -61,6 +58,13 @@ export function fromNodeRequest(
(init as { duplex: "half" }).duplex = "half";
}

// Abort action/loaders once we can no longer write a response iff we have
// not yet sent a response (i.e., `close` without `finish`)
// `finish` -> done rendering the response
// `close` -> response can no longer be written to
nodeRes.on("finish", () => (controller = null));
nodeRes.on("close", () => controller?.abort());

return new Request(url.href, init);
}

Expand Down
12 changes: 8 additions & 4 deletions packages/remix-express/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ export function createRemixRequest(
// Use `req.originalUrl` so Remix is aware of the full path
let url = new URL(`${req.protocol}://${resolvedHost}${req.originalUrl}`);

// Abort action/loaders once we can no longer write a response
let controller = new AbortController();
res.on("close", () => controller.abort());

let controller: AbortController | null = new AbortController();
let init: RequestInit = {
method: req.method,
headers: createRemixHeaders(req.headers),
Expand All @@ -112,6 +109,13 @@ export function createRemixRequest(
(init as { duplex: "half" }).duplex = "half";
}

// Abort action/loaders once we can no longer write a response iff we have
// not yet sent a response (i.e., `close` without `finish`)
// `finish` -> done rendering the response
// `close` -> response can no longer be written to
res.on("finish", () => (controller = null));
res.on("close", () => controller?.abort());

return new Request(url.href, init);
}

Expand Down