Skip to content

Bound and drain hand-rolled HTTP transport pools - #6495

Merged
tgrunnagle merged 6 commits into
mainfrom
alert-tamarillo
Sep 3, 2026
Merged

tgrunnagle merged 6 commits into
mainfrom
alert-tamarillo

Conversation

@tgrunnagle

@tgrunnagle tgrunnagle commented Sep 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

#6480 bounded the connection pool in networking.HttpClientBuilder.Build, but
several hand-rolled &http.Transport{} literals outside the builder set only
their two timeouts and left IdleConnTimeout at zero, so an idle pooled
connection never expires — a dropped client pins a socket and its goroutine pair
for the process lifetime. Separately, a wrapping RoundTripper that does not
implement and forward CloseIdleConnections silently makes
http.Client.CloseIdleConnections a no-op, re-hiding the pool that #6480 fixed.
This PR closes both gaps for the sites called out in the issue.

  • Bound the pools: add networking.SetIdleConnBounds, one helper that sets
    IdleConnTimeout/MaxIdleConns/MaxIdleConnsPerHost together, and route
    Build and the hand-rolled transports through it so the bounds have a single
    source of truth (no more per-site literals that can drift).
  • Make the bounds testable: extract each inline transport in
    pkg/auth/discovery and pkg/auth/oauth into a small package-private builder
    that calls SetIdleConnBounds, so the exact sites Bound the hand-rolled http.Transport pools outside networking.Build #6483 names are covered by
    unit tests rather than relying on a "mirrors Build" comment.
  • Forward the drain: give each wrapping RoundTripper a
    CloseIdleConnections that forwards to its base via networking.ForwardCloseIdle,
    so the call reaches the concrete *http.Transport at the bottom of the chain.
    This includes the session-backed mcp_session.go twins, whose doc requires the
    invariant be kept in sync with the canonical wrappers until [vMCP] Consolidate duplicate identity-propagation round-trippers into shared internal transport package #5333.
  • Guard against silent regression: add var _ networking.IdleConnectionCloser
    compile-time assertions to every wrapper, so a rename or typo of
    CloseIdleConnections fails the build instead of re-hiding the pool.

Closes #6483

Type of change

  • Bug fix

Test plan

  • Unit tests (task test)
  • Linting (task lint-fix)

New unit tests cover both halves of the fix:

  • Bounds — TestSetIdleConnBounds pins the shared helper's values
    (pkg/networking/http_client_test.go); per-site tests assert the built client
    or transport carries the three fields, including both dial modes for the
    SSRF-guarded builders (pkg/auth/discovery/discovery_test.go,
    pkg/auth/oauth/oidc_test.go, pkg/oauthproto/dcr_test.go,
    pkg/oauthproto/discovery_test.go).
  • Forwarding — spy RoundTripper tests assert each wrapper forwards
    CloseIdleConnections to its base, is a safe no-op when the base lacks the
    capability, and does not panic on a nil base
    (pkg/auth/dcr/resolver_test.go, pkg/oauthproto/useragent_test.go,
    pkg/registry/auth/transport_test.go); TestForwardCloseIdle covers the
    helper directly (pkg/networking/http_client_test.go).

Changes

File Change
pkg/networking/http_client.go Add SetIdleConnBounds and ForwardCloseIdle helpers; route Build through SetIdleConnBounds; var _ assertions on ValidatingTransport/closeIdlerTransport
pkg/auth/discovery/discovery.go Extract newDetectionClient and newResourceMetadataTransport, both calling SetIdleConnBounds
pkg/auth/oauth/oidc.go Extract newOIDCDiscoveryTransport, calling SetIdleConnBounds
pkg/oauthproto/constants.go Add the local idle-pool const block for the two leaf sites (cannot import networking)
pkg/oauthproto/dcr.go, pkg/oauthproto/discovery.go Reference the local pool consts instead of inline literals
pkg/auth/dcr/resolver.go Forward CloseIdleConnections from bearerTokenTransport; var _ assertion
pkg/registry/auth/transport.go Forward CloseIdleConnections from the auth Transport; var _ assertion
pkg/oauthproto/useragent.go Forward CloseIdleConnections from UserAgentTransport; local-shape var _ assertion
pkg/vmcp/client/client.go Forward via ForwardCloseIdle; var _ assertions on the three wrappers
pkg/vmcp/headerforward/transport.go Forward via ForwardCloseIdle; var _ assertion
pkg/vmcp/session/internal/backend/mcp_session.go Forward CloseIdleConnections from the authRoundTripper/identityRoundTripper twins; var _ assertions

Does this introduce a user-facing change?

No. This is an internal resource-lifecycle fix with no change to public APIs or
observable behavior.

Special notes for reviewers

  • pkg/authz/authorizers/http/http_client.go is intentionally NOT changed.
    The issue lists it under the CloseIdleConnections wrappers, but it uses
    http.DefaultTransport (or a bounded Clone) with no swallowing wrapper — its
    pool is already bounded and its CloseIdleConnections already reaches the
    pool. Flagging so anyone cross-checking the issue's site list does not read it
    as a missed case.
  • pkg/oauthproto cannot import pkg/networking (networking imports
    oauthproto, so it would cycle). Its two leaf transports therefore reference a
    local const block in constants.go that mirrors the networking.Build bounds
    by hand, and UserAgentTransport.CloseIdleConnections uses the equivalent
    inline interface{ CloseIdleConnections() } assertion instead of
    ForwardCloseIdle. Both carry a comment explaining the cycle.
  • The bound values (IdleConnTimeout: 90s, MaxIdleConns: 100,
    MaxIdleConnsPerHost: 4) live once in networking's constants (mirrored in
    oauthproto for the cycle-blocked sites). Routing these hand-rolled transports
    through the builder would be the better long-term fix but is a larger refactor
    left out of scope here.
  • The server/discovery/health.go unix/npipe transports are left as a follow-up
    (local IPC sockets, a different leak profile than the network pools this PR
    targets).

Generated with Claude Code

#6480 bounded and drained only the clients built by
networking.HttpClientBuilder.Build. Several hand-rolled
&http.Transport{} literals elsewhere left IdleConnTimeout at zero, so a
pooled idle connection never expired and a dropped client pinned a
socket plus its goroutine pair for the process lifetime. Separately,
several wrapping RoundTrippers did not implement CloseIdleConnections,
so http.Client.CloseIdleConnections type-asserted the outermost
transport, missed the method, and silently became a no-op.

Implements changes for issue #6483:
- Bound the transport literals in pkg/auth/discovery, pkg/auth/oauth,
  and pkg/oauthproto (discovery, dcr) with the same host-scoped pool
  bounds Build applies
- Add networking.ForwardCloseIdle so a wrapping RoundTripper forwards
  CloseIdleConnections without an ad-hoc anonymous assertion; use it
  from ValidatingTransport and the vmcp wrappers
- Forward CloseIdleConnections from bearerTokenTransport,
  registry/auth.Transport, and oauthproto.UserAgentTransport (the last
  inline, since pkg/oauthproto cannot import pkg/networking)

pkg/authz/authorizers/http builds no wrapper and uses http.DefaultTransport
(or its Clone), which is already bounded and forwards the call, so it needs
no change.
@github-actions github-actions Bot added the size/S Small PR: 100-299 lines changed label Sep 2, 2026
@codecov

codecov Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.22642% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.26%. Comparing base (da85cdb) to head (ee54589).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
pkg/vmcp/session/internal/backend/mcp_session.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6495   +/-   ##
=======================================
  Coverage   78.25%   78.26%           
=======================================
  Files         770      770           
  Lines       75389    75456   +67     
=======================================
+ Hits        58996    59052   +56     
- Misses      16388    16399   +11     
  Partials        5        5           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

jhrozek
jhrozek previously approved these changes Sep 3, 2026

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the pool-bounding and CloseIdleConnections-forwarding changes across all touched transports/wrappers. Verified the 90s/100/4 constants match networking.Build's existing defaults, the UserAgentTransport import-cycle rationale checks out, and each production change has a matching test. LGTM.

@tgrunnagle tgrunnagle left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multi-agent review — 4 specialists (concurrency/lifecycle, Go quality, test coverage, security). Codex cross-review skipped (CLI not installed).

Solid, correctly-scoped completion of #6480 / #6483. The fix is correct, SSRF-neutral (verified: the private-IP dialer only exists in the blockPrivateIPs branch, which also sets DisableKeepAlives, so pooling never skips a per-dial check), and the import-cycle constraints are correctly identified and documented. No blocking issues. Four MEDIUM quality/robustness items below are worth addressing before merge; none is a correctness bug in the shipped path.

# Severity Finding
F1 MEDIUM Pool values (90s/100/4) hardcoded across 5 sites; 3 already import networking and could use exported constants
F2 MEDIUM 3 of 5 bounded transports (discovery.go ×2, oidc.go) have no test
F3 MEDIUM nil-Base NotPanics tests pass even if the fallback they claim to verify were removed
F4 MEDIUM mcp_session.go "twin" wrappers (documented "keep in sync") still swallow CloseIdleConnections
F5 LOW New wrappers lack var _ IdleConnectionCloser compile-time assertions
F6 LOW UserAgentTransport no-capability branch untested
F7 LOW server/discovery/health.go unix/npipe transports unbounded (out of scope — follow-up)

INFO (no action): the UserAgentTransport inline anonymous assertion is justified (real networking→oauthproto cycle); closeIdleSpy per-package duplication is idiomatic.

Generated with Claude Code

Comment thread pkg/oauthproto/dcr.go Resolved
Comment thread pkg/auth/discovery/discovery.go Resolved
Comment thread pkg/oauthproto/useragent_test.go Resolved
Comment thread pkg/vmcp/client/client.go Resolved
Comment thread pkg/registry/auth/transport.go Resolved
Comment thread pkg/oauthproto/useragent_test.go Resolved
Addresses #6495 review comments:
- MEDIUM pkg/oauthproto/dcr.go (3925404488): the 90s/100/4 pool values
  were duplicated across five transport literals with only a comment to
  keep them in sync. Add networking.SetIdleConnBounds as the single
  source (Build now uses it too); the three cycle-free sites call it, and
  the two oauthproto leaf sites reference a local const block since
  pkg/networking imports pkg/oauthproto.
- MEDIUM pkg/auth/discovery/discovery.go (3925404500): the two
  discovery.go transports and the oidc.go transport shipped untested.
  Extract each into a small package-private builder that calls
  SetIdleConnBounds, and assert the three bounds (and that blockPrivateIPs
  disables keep-alives) in unit tests.

Also add compile-time IdleConnectionCloser assertions to networking's own
ValidatingTransport and closeIdlerTransport wrappers.
Addresses #6495 review comments:
- MEDIUM pkg/vmcp/client/client.go (3925404525): the session-backed twins
  authRoundTripper and identityRoundTripper in mcp_session.go implemented
  RoundTrip but not CloseIdleConnections, so they swallowed the drain the
  canonical twins forward. Their doc requires the invariant be kept in sync
  until #5333; mirror the forward with networking.ForwardCloseIdle.
- LOW pkg/registry/auth/transport.go (3925404536): none of the new
  forwarding wrappers carried a compile-time assertion, so a rename or typo
  of CloseIdleConnections would compile and re-hide the pool. Add
  var _ networking.IdleConnectionCloser assertions to the wrappers
  (registry/auth.Transport, bearerTokenTransport, the vmcp wrappers, and
  the twins); UserAgentTransport asserts the local shape since pkg/oauthproto
  cannot import pkg/networking.
Addresses #6495 review comments:
- MEDIUM pkg/oauthproto/useragent_test.go (3925404517): the nil-Base test
  claimed to verify the DefaultTransport fallback forwards, but a type
  assertion on a nil interface returns ok==false without panicking, so the
  test would still pass if the guard were removed. Soften the comment to
  state accurately that it only checks the nil-Base path does not panic.
- LOW pkg/oauthproto/useragent_test.go (3925404540): the ok==false arm
  (a non-nil Base lacking CloseIdleConnections) was never exercised. Add a
  plainRoundTripper sub-case asserting a safe no-op.
@tgrunnagle

Copy link
Copy Markdown
Collaborator Author

F7 (LOW) — the server/discovery/health.go unix/npipe transports are left for a follow-up, as noted in the review. Those are local IPC sockets (not network pools), so the leak profile differs; tracking separately keeps this PR scoped to #6483.

@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/S Small PR: 100-299 lines changed labels Sep 3, 2026
@tgrunnagle
tgrunnagle marked this pull request as ready for review September 3, 2026 15:56
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels Sep 3, 2026
Codespell flags the plural "keep-alives"; the rest of the tree uses the
singular. Reword the comments and test messages added in this branch to
match, unblocking the Spellcheck CI job.
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels Sep 3, 2026
@tgrunnagle
tgrunnagle merged commit fd1e7b5 into main Sep 3, 2026
48 checks passed
@tgrunnagle
tgrunnagle deleted the alert-tamarillo branch September 3, 2026 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Medium PR: 300-599 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bound the hand-rolled http.Transport pools outside networking.Build

2 participants