Skip to content

Commit 5f08317

Browse files
committed
netconn: Cleanup contexts on close
Updates #255
1 parent 642a013 commit 5f08317

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Diff for: netconn.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ 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() {
@@ -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)