Skip to content

Commit 45a978c

Browse files
committed
fix close of closed channel
1 parent bfeb1da commit 45a978c

File tree

1 file changed

+11
-3
lines changed
  • agent/agentcontainers/watcher

1 file changed

+11
-3
lines changed

agent/agentcontainers/watcher/noop.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ func NewNoop() Watcher {
1212
}
1313

1414
type noopWatcher struct {
15-
closed chan struct{}
15+
mu synx.Mutex
16+
closed bool
17+
done chan struct{}
1618
}
1719

1820
func (*noopWatcher) Add(string) error {
@@ -27,12 +29,18 @@ func (n *noopWatcher) Next(ctx context.Context) (*fsnotify.Event, error) {
2729
select {
2830
case <-ctx.Done():
2931
return nil, ctx.Err()
30-
case <-n.closed:
32+
case <-n.done:
3133
return nil, xerrors.New("watcher closed")
3234
}
3335
}
3436

3537
func (n *noopWatcher) Close() error {
36-
close(n.closed)
38+
n.mu.Lock()
39+
defer n.mu.Unlock()
40+
if n.closed {
41+
return nil
42+
}
43+
n.closed = true
44+
close(n.done)
3745
return nil
3846
}

0 commit comments

Comments
 (0)