Skip to content

fix(middleware): clear request timeout on close and propagate abort signal downstream (closes #910) - #916

Open
mopxam-art wants to merge 1 commit into
Heliobond:mainfrom
mopxam-art:fix/request-timeout-leak-and-abort
Open

mopxam-art wants to merge 1 commit into
Heliobond:mainfrom
mopxam-art:fix/request-timeout-leak-and-abort

Conversation

@mopxam-art

Copy link
Copy Markdown

Overview

Closes #910 by ensuring that the pending request timer in requestTimeout middleware is cleared immediately when a client aborts/disconnects early (res.once("close")), preventing retained timer accumulation and subsequent write errors against dead responses. Additionally attaches an AbortController signal to req.signal, propagating cancellation to downstream handlers when a timeout expires or when the client disconnects before the response completes.

Changes

  • src/middleware/requestTimeout.ts:
    • Added res.once("close") handler alongside finish to clear the pending timer upon early client disconnect.
    • Instantiated AbortController and attached its signal to (req as RequestWithSignal).signal.
    • Aborted downstream signal on timeout (Request Timeout) and on premature client disconnect (Client disconnected).
    • Added bounds checking in getTimeoutMs to fall back cleanly to defaults (30000ms / 60000ms) on invalid or non-positive environment variables.
  • src/__tests__/requestTimeout-5d-matrix.test.ts:
    • Added 114 tests across the 5D matrix verifying boundaries, timeout configuration, status code lifecycles, client disconnect timer leak prevention, idempotency, and concurrent request stress.

Verification

  • All 114 tests pass green (npx jest src/__tests__/requestTimeout-5d-matrix.test.ts).
  • TypeScript typecheck passes with 0 errors (npx tsc --noEmit).
  • ESLint passes with 0 warnings/errors.
  • Production build succeeds cleanly (npm run build).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

requestTimeout leaks timers on client disconnect and the 408 path does not stop the downstream handler

1 participant