go: net/http: connCount underflow panic with MaxConnsPerHost
I’m splitting this off from https://github.com/golang/go/issues/34941 because it seems to be a separate issue, although with the same symptom.
$ go version go version go1.14 darwin/amd64
I compiled this binary on macOS, but I’m running it on Linux amd64.
Here is the stack trace:
panic: net/http: internal error: connCount underflow
goroutine 2114 [running]:
net/http.(*Transport).decConnsPerHost(0x1094ca0, 0x0, 0x0, 0xc001f110b5, 0x4, 0xc001a27320, 0x14, 0x0)
/usr/local/go/src/net/http/transport.go:1391 +0x590
net/http.(*persistConn).closeLocked(0xc0000cd320, 0xb8a620, 0xc000096b60)
/usr/local/go/src/net/http/transport.go:2584 +0xf5
net/http.(*persistConn).close(0xc0000cd320, 0xb8a620, 0xc000096b60)
/usr/local/go/src/net/http/transport.go:2574 +0x83
net/http.(*persistConn).readLoop.func1(0xc0000cd320, 0xc0073b4d60)
/usr/local/go/src/net/http/transport.go:1946 +0x41
net/http.(*persistConn).readLoop(0xc0000cd320)
/usr/local/go/src/net/http/transport.go:2122 +0x12ae
created by net/http.(*Transport).dialConn
/usr/local/go/src/net/http/transport.go:1647 +0xc56
The usage is in a forward proxy server (github.com/andybalholm/redwood). We have about 2000 users running through the server I have been watching these errors on the most closely. So the usage is heavy, and probably about as close to random as you are likely to find.
I thought that switching to two Transports (one for HTTP/1.1 and one for HTTP/2) instead of one http.Transport that was configured to automatically switch protocols would get rid of the panics. But it didn’t seem to help. (So it seems that the panic doesn’t depend on having HTTP/2 enabled on the Transport.)
I had Transport.MaxConnsPerHost set to 8. Setting it to 0 (unlimited) made the panics go away.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 19 (17 by maintainers)
I can’t run it with -race on the server where I was getting the panics; it’s too heavily loaded. But I tried it on a local server with just me proxied through it, and that was enough:
It’s been several years since I tried running with -race. I didn’t find anything then, and I figured only a heavily-loaded production server would have enough concurrency to expose races if there were any. But now I know that running with -race locally is worth doing.
Reordering the startup steps gets rid of the race.
@andybalholm I am confused. You mention that you set MaxConnsPerHost = 8, but the above does not show you setting it. Where/When did you do that.
@bcmills Here is my transport:
The environment doesn’t specify a proxy. I call
http2.ConfigureTransport(httpTransport)elsewhere in the code. (But not making that call doesn’t fix the problem.)Building with
I still get the panic: