Skip to content

Commit

Permalink
fix: if the only stream in a connection times out, prevent re-use to …
Browse files Browse the repository at this point in the history
…abandon the connection.

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.
  • Loading branch information
btasker committed Apr 18, 2023
1 parent eb1572c commit db3e8a7
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 db3e8a7

Please sign in to comment.