Skip to content

Cancel connection attempts after queued requests abort - #5662

Closed
dills122 wants to merge 3 commits into
nodejs:mainfrom
dills122:codex/fix-386-abort-while-connecting
Closed

Cancel connection attempts after queued requests abort#5662
dills122 wants to merge 3 commits into
nodejs:mainfrom
dills122:codex/fix-386-abort-while-connecting

Conversation

@dills122

@dills122 dills122 commented Aug 7, 2026

Copy link
Copy Markdown

Summary

Fixes #386 by rejecting requests that abort while connection establishment is still pending and cancelling connection attempts once no queued request needs them.

Previously, abort handling was attached when a request reached onRequestStart. A request waiting behind an unresolved connector had not reached that point, so aborting its signal did not settle the request until the connector eventually succeeded or failed.

What Changed

  • Reject pre-aborted requests without starting a connection.
  • Bind the existing public API abort listener to requests while they are still queued.
  • Cover request, stream, pipeline, connect, and upgrade consistently.
  • Give each request-driven connection attempt its own AbortSignal and abort it after every request depending on that attempt has been removed.
  • Preserve intentional internal preconnect attempts that have no queued request.
  • Ignore late callbacks from cancelled attempts and destroy any stale socket they return.
  • Keep intentional cancellation silent instead of emitting connectionError and causing pools to evict an otherwise usable client.
  • Document and type the optional connector signal.

Validation

  • npm run lint
  • npm run test:typescript
  • Full test/*.js unit corpus
  • Full test/node-test/**/*.js corpus
  • Full test/interceptors/*.js corpus
  • Focused request, stream, pipeline, connect, upgrade, pool, preconnect, and pipelining regression suites
  • 11 new tests covering exact abort reasons, listener cleanup, shared attempts, retry composition, stale connector callbacks, built-in connector cancellation, and pool lifecycle behavior

Scope Notes

Custom connectors receive the attempt-scoped signal and should stop pending work when it aborts. A connector that ignores the signal cannot have its internal work forcibly stopped, but Undici still settles the request promptly and prevents a late socket or error from affecting a newer attempt.

The cancellation signal represents the lifetime of a connection attempt, not any single request. A shared attempt is cancelled only when all queued requests relying on it are gone.

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.

Client will connect even if all requests are aborted

1 participant