Skip to content

Commit

Permalink
Fix adapter logic for aborting requests (#10046)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 authored Oct 2, 2024
1 parent 38ca648 commit a786f60
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
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

0 comments on commit a786f60

Please sign in to comment.