Skip to content

Commit

Permalink
netconn: Cleanup contexts on close
Browse files Browse the repository at this point in the history
Updates #255
  • Loading branch information
nhooyr committed Jan 9, 2021
1 parent 642a013 commit 5f08317
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions netconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn {
writeMu: newMu(c),
}

var writeCancel context.CancelFunc
nc.writeCtx, writeCancel = context.WithCancel(ctx)
var readCancel context.CancelFunc
nc.readCtx, readCancel = context.WithCancel(ctx)
nc.writeCtx, nc.writeCancel = context.WithCancel(ctx)
nc.readCtx, nc.readCancel = context.WithCancel(ctx)

nc.writeTimer = time.AfterFunc(math.MaxInt64, func() {
if !nc.writeMu.tryLock() {
Expand Down Expand Up @@ -98,11 +96,13 @@ type netConn struct {
writeMu *mu
writeExpired int64
writeCtx context.Context
writeCancel context.CancelFunc

readTimer *time.Timer
readMu *mu
readExpired int64
readCtx context.Context
readCancel context.CancelFunc
readEOFed bool
reader io.Reader
}
Expand All @@ -111,7 +111,9 @@ var _ net.Conn = &netConn{}

func (nc *netConn) Close() error {
nc.writeTimer.Stop()
nc.writeCancel()
nc.readTimer.Stop()
nc.readCancel()
return nc.c.Close(StatusNormalClosure, "")
}

Expand Down

0 comments on commit 5f08317

Please sign in to comment.