8000 add close protection to fsnotify watcher as well · coder/coder@d2721a7 · GitHub
[go: up one dir, main page]

Skip to content

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

agent/agentcontainers/watcher/watcher.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package watcher
88

99
import (
1010
"context"
11+
"sync"
1112

1213
"github.com/fsnotify/fsnotify"
1314
"golang.org/x/xerrors"
@@ -33,7 +34,8 @@ type Watcher interface {
3334

3435
type fsnotifyWatcher struct {
3536
*fsnotify.Watcher
36-
closed chan struct{}
37+
closeOnce sync.Once
38+
closed chan struct{}
3739
}
3840

3941
func NewFSNotify() (Watcher, error) {
@@ -80,10 +82,12 @@ func (f *fsnotifyWatcher) Next(ctx context.Context) (*fsnotify.Event, error) {
8082
}
8183
}
8284

83-
func (f *fsnotifyWatcher) Close() error {
84-
if err := f.Watcher.Close(); err != nil {
85-
return xerrors.Errorf("close watcher: %w", err)
86-
}
87-
close(f.closed)
88-
return nil
85+
func (f *fsnotifyWatcher) Close() (err error) {
86+
f.closeOnce.Do(func() {
87+
if err := f.Watcher.Close(); err != nil {
88+
err = xerrors.Errorf("close watcher: %w", err)
89+
}
90+
close(f.closed)
91+
})
92+
return err
8993
}

0 commit comments

Comments
 (0)
0