Skip to content

Commit 707bc35

Browse files
committed
Fix concurrent access to acceptCh and closeCh.
1 parent 899d13d commit 707bc35

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

service/listeners.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,22 @@ type OnCloseFunc func() error
7373

7474
type virtualStreamListener struct {
7575
addr net.Addr
76+
mu sync.Mutex // Mutex to protect access to the channels
7677
acceptCh <-chan acceptResponse
7778
closeCh chan struct{}
79+
closed bool
7880
onCloseFunc OnCloseFunc
7981
}
8082

8183
var _ StreamListener = (*virtualStreamListener)(nil)
8284

8385
func (sl *virtualStreamListener) AcceptStream() (transport.StreamConn, error) {
86+
sl.mu.Lock()
87+
acceptCh := sl.acceptCh
88+
sl.mu.Unlock()
89+
8490
select {
85-
case acceptResponse, ok := <-sl.acceptCh:
91+
case acceptResponse, ok := <-acceptCh:
8692
if !ok {
8793
return nil, net.ErrClosed
8894
}
@@ -93,8 +99,16 @@ func (sl *virtualStreamListener) AcceptStream() (transport.StreamConn, error) {
9399
}
94100

95101
func (sl *virtualStreamListener) Close() error {
102+
sl.mu.Lock()
103+
if sl.closed {
104+
sl.mu.Unlock()
105+
return nil
106+
}
107+
sl.closed = true
96108
sl.acceptCh = nil
97109
close(sl.closeCh)
110+
sl.mu.Unlock()
111+
98112
if sl.onCloseFunc != nil {
99113
return sl.onCloseFunc()
100114
}

0 commit comments

Comments
 (0)