]> Cypherpunks repositories - gostls13.git/commit
[release-branch.go1.25] net/http: avoid connCount underflow race
authorDamien Neil <dneil@google.com>
Mon, 15 Sep 2025 22:18:57 +0000 (15:18 -0700)
committerCarlos Amedee <carlos@golang.org>
Wed, 1 Oct 2025 19:10:04 +0000 (12:10 -0700)
commit28ac8d210426f374f69650958bbb20ff800e0aea
tree1c466a617c40b11740e5b46bc4b5521bd65cf81d
parent06993c7721600e35a28aa032081fe2d37690de5d
[release-branch.go1.25] net/http: avoid connCount underflow race

Remove a race condition in counting the number of connections per host,
which can cause a connCount underflow and a panic.

The race occurs when:

  - A RoundTrip call attempts to use a HTTP/2 roundtripper (pconn.alt != nil)
    and receives an isNoCachedConn error. The call removes the pconn from
    the idle conn pool and decrements the connCount for its host.
  - A second RoundTrip call on the same pconn succeeds,
    and delivers the pconn to a third RoundTrip waiting for a conn.
  - The third RoundTrip receives the pconn at the same moment its request
    context is canceled. It places the pconn back into the idle conn pool.

At this time, the connCount is incorrect, because the conn returned to
the idle pool is not matched by an increment in the connCount.

Fix this by not adding HTTP/2 pconns back to the idle pool in
wantConn.cancel.

For #61474
Fixes #75539

Change-Id: I104d6cf85a54d0382eebf3fcf5dda99c69a7c3f6
Reviewed-on: https://go-review.googlesource.com/c/go/+/703936
Auto-Submit: Damien Neil <dneil@google.com>
Reviewed-by: Nicholas Husin <husin@google.com>
Reviewed-by: Nicholas Husin <nsh@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
(cherry picked from commit 3203a5da290753e5c7aceb12f41f06b272356bd0)
Reviewed-on: https://go-review.googlesource.com/c/go/+/705376
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/net/http/transport.go
src/net/http/transport_test.go