Skip to content

bug: node-http adapter aborts every request's signal after a normal response #7663

Closed

Description

Provide environment information

@trpc/server: 11.19.0 (also 11.x in a Fastify app via @trpc/server/adapters/fastify)
Node.js: v24.21.0
OS: Linux x64

Describe the bug

incomingMessageToRequest in the node-http adapter (which the standalone, Express and Fastify adapters all go through) aborts the request's AbortController from res.once('close', …).
close fires on every response, including one that finished normally, so every successful request ends with ac.abort(), and opts.signal in a procedure reports an abort for a request that the client never cancelled.

That has two costs:

  • A listener on signal for "the client went away" also fires after every successful call, so it cannot tell a cancelled request from a completed one.
  • abort() builds a DOMException with a captured stack each time. Profiling a Fastify app under load, DOMException alone was 2.7% of CPU on a trivial query.

I'd expect the signal to abort only when the connection closes before the response has been written.

Link to reproduction

Inline below (no dependencies beyond @trpc/server).

To reproduce

pnpm init -y
pnpm install --save-exact @trpc/[email protected]

Save as repro.mjs and run node repro.mjs:

import { initTRPC } from '@trpc/server';
import { createHTTPServer } from '@trpc/server/adapters/standalone';
import pkg from '@trpc/server/package.json' with { type: 'json' };
const { version } = pkg;

const t = initTRPC.create();
let completed = 0;
let abortedAfterCompleting = 0;

const appRouter = t.router({
	ping: t.procedure.query(({ signal }) => {
		signal?.addEventListener('abort', () => abortedAfterCompleting++, { once: true });
		completed++;
		return 'pong';
	}),
});

const server = createHTTPServer({ router: appRouter }).listen(0);
const { port } = server.address();

for (let i = 0; i < 1000; i++) {
	const response = await fetch(`http://127.0.0.1:${port}/ping`);
	await response.text();
}
await new Promise((resolve) => setTimeout(resolve, 100));

console.log(`@trpc/server ${version}, node ${process.version}`);
console.log(`${completed} requests completed normally; the signal aborted after ${abortedAfterCompleting} of them`);
server.close();

Output:

@trpc/server 11.19.0, node v24.21.0
1000 requests completed normally; the signal aborted after 1000 of them

Additional information

Guarding the abort with res.writableFinished fixes it in our testing:

   const onAbort = () => {
     res.off('close', onAbort);
     req.off('aborted', onAbort);

     // abort the request
-    ac.abort();
+    if (!res.writableFinished) ac.abort();
   };

With that change:

  • the reproduction above reports 0 aborts out of 1000;
  • a client that disconnects mid-request still aborts the procedure's signal (checked with a 2-second query and a fetch aborted after 100 ms, before and after the change);
  • a standalone server with one trivial query, pinned to one core with 50 concurrent connections, goes from about 6,960 to 8,460 requests/s (two runs each: 7,017 / 6,907 before, 8,528 / 8,390 after).

I haven't checked whether anything relies on the signal firing after a completed response as a cleanup hook; streamed responses that are cut off would still close before writableFinished, so they keep their abort.

Activity

  1. pipe1os commented on Oct 16, 2026

    @pipe1os
    Contributor

    The sse.maxDurationMs timer in resolveResponse is only cleared when the request signal aborts, so today it depends on this spurious abort. With #7664 or #7665, every subscription that ends on its own would leave the timer pending for the full maxDurationMs. #7666 clears it when the SSE stream ends. The fetch adapter already leaks it on main.

  2. added a commit that references this issue on Oct 19, 2026
    78ac2eb
  3. github-actions commented on Oct 21, 2026

    @github-actions
    Contributor

    This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions