Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions pkg/watch/watcher_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"os"
"path/filepath"
"sync"
"time"

"github.com/fsnotify/fsevents"
Expand All @@ -38,6 +39,7 @@ type fseventNotify struct {
stop chan struct{}

pathsWereWatching map[string]any
closeOnce sync.Once
}

func (d *fseventNotify) loop() {
Expand Down Expand Up @@ -81,6 +83,8 @@ func (d *fseventNotify) Start() error {
return nil
}

d.closeOnce = sync.Once{}

numberOfWatches.Add(int64(len(d.stream.Paths)))

err := d.stream.Start()
Expand All @@ -92,11 +96,13 @@ func (d *fseventNotify) Start() error {
}

func (d *fseventNotify) Close() error {
numberOfWatches.Add(int64(-len(d.stream.Paths)))
d.closeOnce.Do(func() {
numberOfWatches.Add(int64(-len(d.stream.Paths)))

d.stream.Stop()
close(d.errors)
close(d.stop)
d.stream.Stop()
close(d.errors)
close(d.stop)
})

return nil
}
Expand Down
48 changes: 48 additions & 0 deletions pkg/watch/watcher_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//go:build fsnotify

/*
Copyright 2020 Docker Compose CLI authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package watch

import (
"testing"

"gotest.tools/v3/assert"
)

func TestFseventNotifyCloseIdempotent(t *testing.T) {
// Create a watcher with a temporary directory
tmpDir := t.TempDir()
watcher, err := newWatcher([]string{tmpDir})
assert.NilError(t, err)

// Start the watcher
err = watcher.Start()
assert.NilError(t, err)

// Close should work the first time
err = watcher.Close()
assert.NilError(t, err)

// Close should be idempotent - calling it again should not panic
err = watcher.Close()
assert.NilError(t, err)

// Even a third time should be safe
err = watcher.Close()
assert.NilError(t, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the test should also run Start after this, and try Close again?

}