Skip to content

🐛 Abort the request that completes after connectTimeout in IOHttpClientAdapter - #2646

Open
levili-T wants to merge 1 commit into
cfug:mainfrom
levili-T:fix/connect-timeout-leaks-pooled-connection
Open

levili-T wants to merge 1 commit into
cfug:mainfrom
levili-T:fix/connect-timeout-leaks-pooled-connection

Conversation

@levili-T

@levili-T levili-T commented Dec 1, 2026

Copy link
Copy Markdown

Fixes #2645.

When connectTimeout fires, IOHttpClientAdapter stops waiting for HttpClient.openUrl, but the call keeps running and cannot be cancelled. If it completes later, the returned HttpClientRequest is never closed, so its connection is never released. With HttpClient.maxConnectionsPerHost set, such requests can take every slot of a host, and all later requests to it fail with connectionTimeout until the client is recreated. The issue has the details and a pure Dart repro.

The fix aborts that request when it arrives. Nothing changes for requests that do not time out, and the exception thrown on timeout is the same as before.

This touches timeout handling and connection pooling, which AGENTS.md lists as a sensitive area, so cc @AlexV525.

New Pull Request Checklist

  • I have read the Documentation
  • I have read the Agent Contribution Guidelines (required if any part of the change was produced with AI assistance)
  • I have searched for a similar pull request in the project and found none
  • I have updated this branch with the latest main branch to avoid conflicts (via merge from master or rebase)
  • I have added the required tests to prove the fix/feature I'm adding
  • I have updated the documentation (if necessary)
  • I have run the tests without failures
  • I have updated the CHANGELOG.md in the corresponding package

Additional context and info (if any)

Tests. The new test in dio/test/timeout_test.dart fails without the change (the last request ends with connectionTimeout) and passes with it.

Locally, on Dart 3.12.2, I ran dart format, dart analyze and dart test for the dio package only. dart test ends with 251 passed and 3 failed. The same 3 fail on an unmodified main on my network, they all rely on a non-routable address:

  • options_timeout_integration_test.dart: Options.connectTimeout is used by adapter
  • test_suite_test.dart: Timeout exception of connectTimeout throws
  • test_suite_test.dart: ignores zero duration timeouts

That is why the "tests without failures" box is not ticked. I did not run the min SDK, web or Flutter suites.

No documentation change is needed, the public API is the same.

AI disclosure. The implementation, the test and this description were drafted with Claude Code (Claude) and reviewed by me.

…lientAdapter`

When `connectTimeout` fires, the adapter stops waiting for
`HttpClient.openUrl`, but the call keeps running and cannot be
cancelled. If it completes later, e.g. after waiting for a free slot
under `HttpClient.maxConnectionsPerHost`, the returned request holds a
pooled connection that is never released. Enough of them occupy every
slot of a host, and all later requests to it fail with
`connectionTimeout` until the client is recreated.

Abort that request when it arrives so the connection is released.

Co-Authored-By: Claude <noreply@anthropic.com>
@levili-T
levili-T requested a review from a team as a code owner December 1, 2026 07:42
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.

IOHttpClientAdapter: a request that hits connectTimeout while queued leaks a pooled connection

1 participant