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(server-runtime: fix request signal not aborting after request is garbage collected #10030

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/stale-tips-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/server-runtime": patch
---

Fix request signal not aborting after request is garbage collected
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@
- jvnm-dev
- jwaltz
- jwnx
- jvaill
- kalch
- kamtugeza
- kandros
Expand Down
32 changes: 23 additions & 9 deletions packages/remix-server-runtime/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ export async function callRouteAction({
singleFetch: boolean;
}) {
let result = await action({
request: singleFetch
? stripRoutesParam(stripIndexParam(request))
: stripDataParam(stripIndexParam(request)),
request: makeRequestSignalGcSafe(
singleFetch
? stripRoutesParam(stripIndexParam(request))
: stripDataParam(stripIndexParam(request))
),
context: loadContext,
params,
});
Expand Down Expand Up @@ -81,9 +83,11 @@ export async function callRouteLoader({
singleFetch: boolean;
}) {
let result = await loader({
request: singleFetch
? stripRoutesParam(stripIndexParam(request))
: stripDataParam(stripIndexParam(request)),
request: makeRequestSignalGcSafe(
singleFetch
? stripRoutesParam(stripIndexParam(request))
: stripDataParam(stripIndexParam(request))
),
context: loadContext,
params,
});
Expand Down Expand Up @@ -113,6 +117,16 @@ export async function callRouteLoader({
return isResponse(result) ? result : json(result);
}

function makeRequestSignalGcSafe(request: Request) {
// `undici` wraps the signal passed to the `Request` constructor. When the
// request object is garbage collected, the signal stops working. This is
// problematic when a loader or action is waiting for a signal to be aborted.
// To fix this, we hold a reference to the request in the signal itself so
// that the request isn't garbage collected while the signal is in use.
Object.defineProperty(request.signal, "__request", { value: request });
return request;
}

// TODO: Document these search params better
// and stop stripping these in V2. These break
// support for running in a SW and also expose
Expand All @@ -136,7 +150,7 @@ function stripIndexParam(request: Request) {
method: request.method,
body: request.body,
headers: request.headers,
signal: request.signal,
signal: makeRequestSignalGcSafe(request).signal,
};

if (init.body) {
Expand All @@ -153,7 +167,7 @@ function stripDataParam(request: Request) {
method: request.method,
body: request.body,
headers: request.headers,
signal: request.signal,
signal: makeRequestSignalGcSafe(request).signal,
};

if (init.body) {
Expand All @@ -170,7 +184,7 @@ function stripRoutesParam(request: Request) {
method: request.method,
body: request.body,
headers: request.headers,
signal: request.signal,
signal: makeRequestSignalGcSafe(request).signal,
};

if (init.body) {
Expand Down