Skip to content

Commit 482f584

Browse files
committed
netconn.go: Cleanup contexts on close
Updates #255
1 parent 642a013 commit 482f584

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

netconn.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,14 @@ func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn {
5050
writeMu: newMu(c),
5151
}
5252

53-
var writeCancel context.CancelFunc
54-
nc.writeCtx, writeCancel = context.WithCancel(ctx)
55-
var readCancel context.CancelFunc
56-
nc.readCtx, readCancel = context.WithCancel(ctx)
53+
nc.writeCtx, nc.writeCancel = context.WithCancel(ctx)
54+
nc.readCtx, nc.readCancel = context.WithCancel(ctx)
5755

5856
nc.writeTimer = time.AfterFunc(math.MaxInt64, func() {
5957
if !nc.writeMu.tryLock() {
6058
// If the lock cannot be acquired, then there is an
6159
// active write goroutine and so we should cancel the context.
62-
writeCancel()
60+
nc.writeCancel()
6361
return
6462
}
6563
defer nc.writeMu.unlock()
@@ -75,7 +73,7 @@ func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn {
7573
if !nc.readMu.tryLock() {
7674
// If the lock cannot be acquired, then there is an
7775
// active read goroutine and so we should cancel the context.
78-
readCancel()
76+
nc.readCancel()
7977
return
8078
}
8179
defer nc.readMu.unlock()
@@ -98,11 +96,13 @@ type netConn struct {
9896
writeMu *mu
9997
writeExpired int64
10098
writeCtx context.Context
99+
writeCancel context.CancelFunc
101100

102101
readTimer *time.Timer
103102
readMu *mu
104103
readExpired int64
105104
readCtx context.Context
105+
readCancel context.CancelFunc
106106
readEOFed bool
107107
reader io.Reader
108108
}
@@ -111,7 +111,9 @@ var _ net.Conn = &netConn{}
111111

112112
func (nc *netConn) Close() error {
113113
nc.writeTimer.Stop()
114+
nc.writeCancel()
114115
nc.readTimer.Stop()
116+
nc.readCancel()
115117
return nc.c.Close(StatusNormalClosure, "")
116118
}
117119

0 commit comments

Comments
 (0)