Skip to content

Commit

Permalink
Fix concurrent access to acceptCh and closeCh.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbruens committed Aug 7, 2024
1 parent 899d13d commit 707bc35
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion service/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,22 @@ type OnCloseFunc func() error

type virtualStreamListener struct {
addr net.Addr
mu sync.Mutex // Mutex to protect access to the channels
acceptCh <-chan acceptResponse
closeCh chan struct{}
closed bool
onCloseFunc OnCloseFunc
}

var _ StreamListener = (*virtualStreamListener)(nil)

func (sl *virtualStreamListener) AcceptStream() (transport.StreamConn, error) {
sl.mu.Lock()
acceptCh := sl.acceptCh
sl.mu.Unlock()

select {
case acceptResponse, ok := <-sl.acceptCh:
case acceptResponse, ok := <-acceptCh:
if !ok {
return nil, net.ErrClosed
}
Expand All @@ -93,8 +99,16 @@ func (sl *virtualStreamListener) AcceptStream() (transport.StreamConn, error) {
}

func (sl *virtualStreamListener) Close() error {
sl.mu.Lock()
if sl.closed {
sl.mu.Unlock()
return nil
}
sl.closed = true
sl.acceptCh = nil
close(sl.closeCh)
sl.mu.Unlock()

if sl.onCloseFunc != nil {
return sl.onCloseFunc()
}
Expand Down

0 comments on commit 707bc35

Please sign in to comment.