Skip to content

Commit db3e8a7

Browse files
committed
fix: if the only stream in a connection times out, prevent re-use to 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.
1 parent eb1572c commit db3e8a7

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

http2/transport.go

+14
Original file line numberDiff line numberDiff line change
@@ -1444,8 +1444,22 @@ func (cs *clientStream) writeRequest(req *http.Request) (err error) {
14441444
respHeaderRecv = nil
14451445
respHeaderTimer = nil // keep waiting for END_STREAM
14461446
case <-cs.abort:
1447+
// If this was the only active stream, mark the connection
1448+
// as not for re-use in order to address raciness if the caller
1449+
// tries to call closeIdleConnections() before the stream has been
1450+
// removed
1451+
if len(cc.streams) == 1 {
1452+
cc.doNotReuse = true
1453+
}
14471454
return cs.abortErr
14481455
case <-ctx.Done():
1456+
// If this was the only active stream, mark the connection
1457+
// as not for re-use in order to address raciness if the caller
1458+
// tries to call closeIdleConnections() before the stream has been
1459+
// removed
1460+
if len(cc.streams) == 1 {
1461+
cc.doNotReuse = true
1462+
}
14491463
return ctx.Err()
14501464
case <-cs.reqCancel:
14511465
return errRequestCanceled

0 commit comments

Comments
 (0)