Skip to content

Commit

Permalink
fix request signal not aborting after request is garbage collected
Browse files Browse the repository at this point in the history
  • Loading branch information
jvaill committed Sep 26, 2024
1 parent 38ca648 commit 35994f9
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions packages/remix-server-runtime/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export async function callRouteAction({
routeId: string;
singleFetch: boolean;
}) {
makeRequestSignalGcSafe(request);

let result = await action({
request: singleFetch
? stripRoutesParam(stripIndexParam(request))
Expand Down Expand Up @@ -80,6 +82,8 @@ export async function callRouteLoader({
routeId: string;
singleFetch: boolean;
}) {
makeRequestSignalGcSafe(request);

let result = await loader({
request: singleFetch
? stripRoutesParam(stripIndexParam(request))
Expand Down Expand Up @@ -113,6 +117,15 @@ 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.
Object.defineProperty(request.signal, "__request", request);
return request.signal;
}

// 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 +149,7 @@ function stripIndexParam(request: Request) {
method: request.method,
body: request.body,
headers: request.headers,
signal: request.signal,
signal: makeRequestSignalGcSafe(request),
};

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

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

if (init.body) {
Expand Down

0 comments on commit 35994f9

Please sign in to comment.