Skip to content

Commit 38a0c5b

Browse files
AlexanderYastrebovgopherbot
authored andcommitted
net/http: clear reference to the request context after transport getConn
Clears wannConn ctx and prevents pending dialConnFor after connection delivered or canceled. Updates #50798 Change-Id: I9a681ac0f222be56571fa768700220f6b5ee0888 GitHub-Last-Rev: fd6c83a GitHub-Pull-Request: #61524 Reviewed-on: https://go-review.googlesource.com/c/go/+/512196 Auto-Submit: Matthew Dempsky <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Reviewed-by: Damien Neil <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent c738344 commit 38a0c5b

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/net/http/transport.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,6 @@ func (t *Transport) dial(ctx context.Context, network, addr string) (net.Conn, e
12051205
type wantConn struct {
12061206
cm connectMethod
12071207
key connectMethodKey // cm.key()
1208-
ctx context.Context // context for dial
12091208
ready chan struct{} // closed when pc, err pair is delivered
12101209

12111210
// hooks for testing to know when dials are done
@@ -1214,7 +1213,8 @@ type wantConn struct {
12141213
beforeDial func()
12151214
afterDial func()
12161215

1217-
mu sync.Mutex // protects pc, err, close(ready)
1216+
mu sync.Mutex // protects ctx, pc, err, close(ready)
1217+
ctx context.Context // context for dial, cleared after delivered or canceled
12181218
pc *persistConn
12191219
err error
12201220
}
@@ -1229,6 +1229,13 @@ func (w *wantConn) waiting() bool {
12291229
}
12301230
}
12311231

1232+
// getCtxForDial returns context for dial or nil if connection was delivered or canceled.
1233+
func (w *wantConn) getCtxForDial() context.Context {
1234+
w.mu.Lock()
1235+
defer w.mu.Unlock()
1236+
return w.ctx
1237+
}
1238+
12321239
// tryDeliver attempts to deliver pc, err to w and reports whether it succeeded.
12331240
func (w *wantConn) tryDeliver(pc *persistConn, err error) bool {
12341241
w.mu.Lock()
@@ -1238,6 +1245,7 @@ func (w *wantConn) tryDeliver(pc *persistConn, err error) bool {
12381245
return false
12391246
}
12401247

1248+
w.ctx = nil
12411249
w.pc = pc
12421250
w.err = err
12431251
if w.pc == nil && w.err == nil {
@@ -1255,6 +1263,7 @@ func (w *wantConn) cancel(t *Transport, err error) {
12551263
close(w.ready) // catch misbehavior in future delivery
12561264
}
12571265
pc := w.pc
1266+
w.ctx = nil
12581267
w.pc = nil
12591268
w.err = err
12601269
w.mu.Unlock()
@@ -1463,8 +1472,12 @@ func (t *Transport) queueForDial(w *wantConn) {
14631472
// If the dial is canceled or unsuccessful, dialConnFor decrements t.connCount[w.cm.key()].
14641473
func (t *Transport) dialConnFor(w *wantConn) {
14651474
defer w.afterDial()
1475+
ctx := w.getCtxForDial()
1476+
if ctx == nil {
1477+
return
1478+
}
14661479

1467-
pc, err := t.dialConn(w.ctx, w.cm)
1480+
pc, err := t.dialConn(ctx, w.cm)
14681481
delivered := w.tryDeliver(pc, err)
14691482
if err == nil && (!delivered || pc.alt != nil) {
14701483
// pconn was not passed to w,

0 commit comments

Comments
 (0)