Skip to content

Commit

Permalink
net/http2: if the only stream in a connection times out, prevent re-use
Browse files Browse the repository at this point in the history
This commit mitigates some raciness which can occur when an established
connection fails. Without it, the connection can continue to be drawn
from the connection pool, leading to subsequent requests failing.

Fixes golang/go#59690
  • Loading branch information
btasker committed Apr 18, 2023
1 parent eb1572c commit 613c39d
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions http2/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -1444,8 +1444,22 @@ func (cs *clientStream) writeRequest(req *http.Request) (err error) {
respHeaderRecv = nil
respHeaderTimer = nil // keep waiting for END_STREAM
case <-cs.abort:
// If this was the only active stream, mark the connection
// as not for re-use in order to address raciness if the caller
// tries to call closeIdleConnections() before the stream has been
// removed
if len(cc.streams) == 1 {
cc.doNotReuse = true
}
return cs.abortErr
case <-ctx.Done():
// If this was the only active stream, mark the connection
// as not for re-use in order to address raciness if the caller
// tries to call closeIdleConnections() before the stream has been
// removed
if len(cc.streams) == 1 {
cc.doNotReuse = true
}
return ctx.Err()
case <-cs.reqCancel:
return errRequestCanceled
Expand Down

0 comments on commit 613c39d

Please sign in to comment.