Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/request-abort-signal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": patch
---

Make `event.request.signal` abort when the client disconnects on Node. The request was built without a signal tied to the connection, so it never aborted and API routes kept running after the client went away. On web runtimes the platform signal still applies.
73 changes: 73 additions & 0 deletions packages/start/src/server/fetchEvent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EventEmitter } from "node:events";
import { H3Event } from "vinxi/http";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { cloneEvent, createFetchEvent, getFetchEvent, mergeResponseHeaders } from "./fetchEvent";
Expand Down Expand Up @@ -319,3 +320,75 @@ describe("fetchEvent", () => {
});
});
});

describe("request abort signal", () => {
const createNodeEvent = (request: Request) => {
const res = Object.assign(new EventEmitter(), { writableEnded: false });
const req = { errored: null as Error | null };
const event = {
node: { req, res },
context: {},
web: { request }
} as unknown as H3Event;
mockedVinxiHttp.getWebRequest.mockReturnValue(request);
return { event, req, res };
};

beforeEach(() => {
vi.clearAllMocks();
});

it("aborts when the response closes before it was sent", () => {
const { event, res } = createNodeEvent(new Request("http://localhost/api"));
const { request } = createFetchEvent(event);
expect(request.signal.aborted).toBe(false);

res.emit("close");

expect(request.signal.aborted).toBe(true);
});

it("does not abort when the response closes after it was sent", () => {
const { event, res } = createNodeEvent(new Request("http://localhost/api"));
const { request } = createFetchEvent(event);

res.writableEnded = true;
res.emit("close");

expect(request.signal.aborted).toBe(false);
});

it("aborts with the request error when the request failed", () => {
const { event, req, res } = createNodeEvent(new Request("http://localhost/api"));
const { request } = createFetchEvent(event);
const error = new Error("socket hang up");

req.errored = error;
res.emit("close");

expect(request.signal.aborted).toBe(true);
expect(request.signal.reason).toBe(error);
});

it("keeps the abort signal of the platform request", () => {
const controller = new AbortController();
const { event } = createNodeEvent(
new Request("http://localhost/api", { signal: controller.signal })
);
const { request } = createFetchEvent(event);

controller.abort();

expect(request.signal.aborted).toBe(true);
});

it("gives later readers the same request, with its body", async () => {
const { event } = createNodeEvent(
new Request("http://localhost/api", { method: "POST", body: "hello" })
);
const { request } = createFetchEvent(event);

expect(event.web!.request).toBe(request);
expect(await request.text()).toBe("hello");
});
});
34 changes: 33 additions & 1 deletion packages/start/src/server/fetchEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,41 @@ import type { FetchEvent, ResponseStub } from "./types";

const fetchEventContext = "solidFetchEvent";

/**
* On Node, the web request vinxi builds has a signal that never aborts, so a
* handler cannot tell when the client goes away. This wires the signal to the
* response closing before it was fully sent. On web runtimes the platform
* request already has a working signal, and it is kept.
*/
function createWebRequest(event: H3Event): Request {
const request = getWebRequest(event);
const res = event.node?.res;
if (!res || typeof res.once !== "function") {
return request;
}
const controller = new AbortController();
res.once("close", () => {
const error = event.node.req.errored;
if (error) {
controller.abort(error);
} else if (!res.writableEnded) {
controller.abort();
}
});
const wrapped = new Request(request, {
signal: AbortSignal.any([request.signal, controller.signal])
});
// The wrapped request takes over the body of the original one, so later
// calls to getWebRequest must get the wrapped request too.
if (event.web) {
event.web.request = wrapped;
}
return wrapped;
}

export function createFetchEvent(event: H3Event): FetchEvent {
return {
request: getWebRequest(event),
request: createWebRequest(event),
response: createResponseStub(event),
clientAddress: getRequestIP(event),
locals: {},
Expand Down
Loading