Skip to content

Commit

Permalink
Add mutex back to protect against concurrent Close() calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbruens committed Sep 18, 2024
1 parent c6e1a0a commit e155ae2
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion service/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ type readRequest struct {

type virtualPacketConn struct {
net.PacketConn
readCh chan readRequest
readCh chan readRequest

mu sync.Mutex // Mutex to protect against race conditions when closing the connection.
closeCh chan struct{}
onCloseFunc OnCloseFunc
}
Expand All @@ -153,7 +155,12 @@ func (pc *virtualPacketConn) ReadFrom(p []byte) (int, net.Addr, error) {
return resp.n, resp.addr, resp.err
}

// Close closes the virtualPacketConn. It must be called once, and only once,
// per virtualPacketConn.
func (pc *virtualPacketConn) Close() error {
pc.mu.Lock()
defer pc.mu.Unlock()

close(pc.closeCh)
if pc.onCloseFunc != nil {
onCloseFunc := pc.onCloseFunc
Expand Down

0 comments on commit e155ae2

Please sign in to comment.