Every request registers a close listener for a request budget most apps never set #74

Closed
opened 2026-08-31 13:52:52 +00:00 by chrnx · 0 comments
Owner

The request gate registers a close listener on every request, including in apps that never set maxConcurrentRequests.

What happens

src/http/server.ts, in the per-request handler:

if (!gate.acquire()) { /* 503 */ }

acquired = true;
res.once('close', () => {
  if (!acquired) return;
  acquired = false;
  gate.release();
});

acquire() returns true unconditionally when the limit is unset:

acquire(): boolean {
  if (limit === undefined || limit <= 0) return true;
  ...
}

So acquired is always true, and the listener is always attached. release() then early-returns for the same unset limit, which makes both the closure and the listener pure overhead for that app.

Why it matters

maxConcurrentRequests is opt-in and defaults to unlimited, so the overwhelming majority of apps pay this and get nothing back. It is a closure allocation and an EventEmitter registration per request, on the hot path, for a feature that is off.

Small in absolute terms. Filed because the pipeline-context work already established that per-request allocations here are worth watching, and because the fix is trivial enough that leaving it undone is a choice rather than a tradeoff.

Scope

Hoist a boolean when the handler is built and guard both the listener and the acquire bookkeeping with it:

const limited = (opts?.limits?.maxConcurrentRequests ?? 0) > 0;

Then the gate does nothing at all — no listener, no flag — for an app that never configured it. Worth checking whether createRequestGate should expose that instead of having the caller re-derive it from opts.

Related

Found reviewing Expressive-Tea/green-tea#23 on GitHub. Deliberately let through so the PR could land: the two findings that mattered (a permanent slot leak, and a 503 that was the only non-JSON error in the tree) were fixed, and this one affects nothing. Taking it here rather than sending the contributor round a fourth time.

The request gate registers a `close` listener on every request, including in apps that never set `maxConcurrentRequests`. ## What happens `src/http/server.ts`, in the per-request handler: ```ts if (!gate.acquire()) { /* 503 */ } acquired = true; res.once('close', () => { if (!acquired) return; acquired = false; gate.release(); }); ``` `acquire()` returns `true` unconditionally when the limit is unset: ```ts acquire(): boolean { if (limit === undefined || limit <= 0) return true; ... } ``` So `acquired` is always `true`, and the listener is always attached. `release()` then early-returns for the same unset limit, which makes both the closure and the listener pure overhead for that app. ## Why it matters `maxConcurrentRequests` is opt-in and defaults to unlimited, so the overwhelming majority of apps pay this and get nothing back. It is a closure allocation and an `EventEmitter` registration per request, on the hot path, for a feature that is off. Small in absolute terms. Filed because the pipeline-context work already established that per-request allocations here are worth watching, and because the fix is trivial enough that leaving it undone is a choice rather than a tradeoff. ## Scope Hoist a boolean when the handler is built and guard both the listener and the acquire bookkeeping with it: ```ts const limited = (opts?.limits?.maxConcurrentRequests ?? 0) > 0; ``` Then the gate does nothing at all — no listener, no flag — for an app that never configured it. Worth checking whether `createRequestGate` should expose that instead of having the caller re-derive it from `opts`. ## Related Found reviewing Expressive-Tea/green-tea#23 on GitHub. Deliberately let through so the PR could land: the two findings that mattered (a permanent slot leak, and a 503 that was the only non-JSON error in the tree) were fixed, and this one affects nothing. Taking it here rather than sending the contributor round a fourth time.
chrnx closed this issue 2026-09-04 13:32:29 +00:00
Sign in to join this conversation.