Skip to content

Conversation

@mitioshi
Copy link

Related issue: #8216

Problem

We use httptrace for our http client. Very frequently we observe traces being marked as "error" due to "http: putIdleConn: too many idle connections for host" in the http.receive span.

This error is a part of an internal mechanism in net/http to pool http connections.
Transport keeps a per-host pool of idle keep-alive sockets.
In tryPutIdleConn, after a request finishes, the connection is returned to that pool. If the pool already holds MaxIdleConnsPerHost entries (default 2 if you don’t override it), the function returns errTooManyIdleHost, and the connection is immediately closed instead of being cached.

This is not a client issue and not even an error(just a part of the pooling flow) and shouldn't mark traces as erroring

Implementation

In this PR, we explicitly ignore the errors that can be passed by http.Transport to the PutIdleConn callback(which is used by ClientTrace). Since the errors are unexported, we have to match the error strings.

I've also added tests for two of three possible erorrs. The third one, errCloseIdle cannot be reliably reproduced in tests without introduing a flaky test, so I decided to omit the test for it.

@mitioshi mitioshi requested review from a team and dmathieu as code owners December 16, 2025 14:06
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Dec 16, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@dmathieu dmathieu added the blocked: CLA Waiting on CLA to be signed before progress can be made label Dec 16, 2025
@mitioshi mitioshi force-pushed the silence-http-pool-errors branch from 492a3b1 to 8320979 Compare December 16, 2025 17:53
@mitioshi
Copy link
Author

@dmathieu Since this PR has a pending CLA label, here's a FYI that I signed it. This PR can now be reviewed

@dmathieu dmathieu removed the blocked: CLA Waiting on CLA to be signed before progress can be made label Dec 18, 2025
// this error is passed to the PutIdleConn callback when http.Transport's HTTP 1.1 connection pool is full
// It's not an error per se, but rather a control flow error used for connection pooling logic in net/http internals
// corresponds to errTooManyIdleHost in net/http/internal/transport.go
const poolReturnErr = "too many idle connections"
Copy link
Member

Choose a reason for hiding this comment

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

Could you move those constants at the package level?

Copy link
Author

Choose a reason for hiding this comment

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

done

// non-nil err in this callback is one of the following net/http errors: errTooManyIdleHost, errBrokenConn, errCloseIdle
// All of those are internal. No reason to mark the span as error if we encounter them
// net/http doesn't export said errors, so we can only match against their strings
if strings.Contains(err.Error(), poolReturnErr) ||
Copy link
Member

Choose a reason for hiding this comment

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

Doing ct.end twice is a bit misleading.
How about checking for err == nil in this condition as well?

Copy link
Author

Choose a reason for hiding this comment

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

done

@codecov
Copy link

codecov bot commented Dec 18, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.3%. Comparing base (9b21944) to head (8320979).

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #8299   +/-   ##
=====================================
  Coverage   82.3%   82.3%           
=====================================
  Files        193     193           
  Lines      13769   13779   +10     
=====================================
+ Hits       11334   11344   +10     
  Misses      2030    2030           
  Partials     405     405           
Files with missing lines Coverage Δ
...on/net/http/httptrace/otelhttptrace/clienttrace.go 84.4% <100.0%> (+0.7%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dmathieu
Copy link
Member

This will need a changelog entry.

@mitioshi
Copy link
Author

This will need a changelog entry.

Added one in the recent commit

@mitioshi mitioshi force-pushed the silence-http-pool-errors branch 2 times, most recently from 9931da9 to b18867d Compare December 22, 2025 11:47
@mitioshi mitioshi force-pushed the silence-http-pool-errors branch from b18867d to 6b277bc Compare December 31, 2025 01:26
@mitioshi
Copy link
Author

@dmathieu FYI: I've implemented the changes proposed by code review

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.

2 participants