Skip to content

Modern Docker Management, Designed for Everyone

fix: improve concurrency handling in watcher and filesystem job

Kyle Mendell6487405b55d9
diff --git a/backend/pkg/fswatch/watcher.go b/backend/pkg/fswatch/watcher.go
index faa01bcf9..e395e564e 100644
--- a/backend/pkg/fswatch/watcher.go
+++ b/backend/pkg/fswatch/watcher.go
@@ -2,11 +2,13 @@ package fswatch
import (
"context"
+ "fmt"
"log/slog"
"os"
"path/filepath"
"runtime"
"strings"
+ "sync"
"time"
"github.com/fsnotify/fsnotify"
@@ -22,6 +24,10 @@ type Watcher struct {
debounce time.Duration
stopCh chan struct{}
stoppedCh chan struct{}
+ mu sync.Mutex
+ started bool
+ stopped bool
+ stopErr error
}
type WatcherOptions struct {
@@ -58,6 +64,16 @@ func NewWatcher(watchPath string, opts WatcherOptions) (*Watcher, error) {
}
func (fw *Watcher) Start(ctx context.Context) error {
+ fw.mu.Lock()
+ defer fw.mu.Unlock()
+
+ if fw.started {
+ return nil
+ }
+ if fw.stopped {
+ return fmt.Errorf("watcher already stopped")
+ }
+
if err := fw.watcher.Add(fw.watchedPath); err != nil {
return err
}
@@ -69,15 +85,36 @@ func (fw *Watcher) Start(ctx context.Context) error {
}
go fw.watchLoop(ctx)
+ fw.started = true
slog.InfoContext(ctx, "Filesystem watcher started", "path", fw.watchedPath)
return nil
}
func (fw *Watcher) Stop() error {
- close(fw.stopCh)
- <-fw.stoppedCh // Wait for watchLoop to finish
- return fw.watcher.Close()
+ fw.mu.Lock()
+ if fw.stopped {
+ err := fw.stopErr
+ fw.mu.Unlock()
+ return err
+ }
+
+ fw.stopped = true
+ started := fw.started
+ fw.mu.Unlock()
+
+ if started {
+ close(fw.stopCh)
+ <-fw.stoppedCh // Wait for watchLoop to finish
+ }
+
+ err := fw.watcher.Close()
+
+ fw.mu.Lock()
+ fw.stopErr = err
+ fw.mu.Unlock()
+
+ return err
}
func (fw *Watcher) watchLoop(ctx context.Context) {
diff --git a/backend/pkg/fswatch/watcher_test.go b/backend/pkg/fswatch/watcher_test.go
index 35ac501da..a82c7b2fd 100644
--- a/backend/pkg/fswatch/watcher_test.go
+++ b/backend/pkg/fswatch/watcher_test.go
@@ -84,3 +84,41 @@ func TestWatcher_StartSkipsExistingSymlinkDirectoriesWhenDisabled(t *testing.T)
case <-time.After(300 * time.Millisecond):
}
}
+
+func TestWatcher_Stop_IsIdempotentAfterStart(t *testing.T) {
+ root := t.TempDir()
+ ctx := t.Context()
+
+ watcher, err := NewWatcher(root, WatcherOptions{})
+ require.NoError(t, err)
+ require.NoError(t, watcher.Start(ctx))
+
+ stopWatcherWithinTimeoutInternal(t, watcher)
+ stopWatcherWithinTimeoutInternal(t, watcher)
+}
+
+func TestWatcher_Stop_IsSafeBeforeStart(t *testing.T) {
+ root := t.TempDir()
+
+ watcher, err := NewWatcher(root, WatcherOptions{})
+ require.NoError(t, err)
+
+ stopWatcherWithinTimeoutInternal(t, watcher)
+ stopWatcherWithinTimeoutInternal(t, watcher)
+}
+
+func stopWatcherWithinTimeoutInternal(t *testing.T, watcher *Watcher) {
+ t.Helper()
+
+ done := make(chan error, 1)
+ go func() {
+ done <- watcher.Stop()
+ }()
+
+ select {
+ case err := <-done:
+ require.NoError(t, err)
+ case <-time.After(2 * time.Second):
+ t.Fatal("timed out waiting for watcher to stop")
+ }
+}
diff --git a/backend/pkg/scheduler/filesystem_watcher_job.go b/backend/pkg/scheduler/filesystem_watcher_job.go
index 9004af8da..f5f95245f 100644
--- a/backend/pkg/scheduler/filesystem_watcher_job.go
+++ b/backend/pkg/scheduler/filesystem_watcher_job.go
@@ -3,6 +3,7 @@ package scheduler
import (
"context"
"log/slog"
+ "sync"
"time"
"github.com/getarcaneapp/arcane/backend/internal/services"
@@ -16,6 +17,7 @@ type FilesystemWatcherJob struct {
settingsService *services.SettingsService
projectsWatcher *fswatch.Watcher
templatesWatcher *fswatch.Watcher
+ mu sync.Mutex
}
func NewFilesystemWatcherJob(
@@ -118,14 +120,21 @@ func (j *FilesystemWatcherJob) Start(ctx context.Context) error {
}
func (j *FilesystemWatcherJob) Stop() error {
+ j.mu.Lock()
+ projectsWatcher := j.projectsWatcher
+ templatesWatcher := j.templatesWatcher
+ j.projectsWatcher = nil
+ j.templatesWatcher = nil
+ j.mu.Unlock()
+
var firstErr error
- if j.projectsWatcher != nil {
- if err := j.projectsWatcher.Stop(); err != nil {
+ if projectsWatcher != nil {
+ if err := projectsWatcher.Stop(); err != nil {
firstErr = err
}
}
- if j.templatesWatcher != nil {
- if err := j.templatesWatcher.Stop(); err != nil && firstErr == nil {
+ if templatesWatcher != nil {
+ if err := templatesWatcher.Stop(); err != nil && firstErr == nil {
firstErr = err
}
}
@@ -158,9 +167,13 @@ func (j *FilesystemWatcherJob) handleTemplatesChange(ctx context.Context) {
func (j *FilesystemWatcherJob) RestartProjectsWatcher(ctx context.Context) error {
slog.InfoContext(ctx, "Restarting projects filesystem watcher")
- // Stop the existing projects watcher if it exists
- if j.projectsWatcher != nil {
- if err := j.projectsWatcher.Stop(); err != nil {
+ j.mu.Lock()
+ oldProjectsWatcher := j.projectsWatcher
+ j.projectsWatcher = nil
+ j.mu.Unlock()
+
+ if oldProjectsWatcher != nil {
+ if err := oldProjectsWatcher.Stop(); err != nil {
slog.WarnContext(ctx, "Failed to stop projects watcher during restart", "error", err)
}
}
@@ -187,13 +200,15 @@ func (j *FilesystemWatcherJob) RestartProjectsWatcher(ctx context.Context) error
return err
}
- j.projectsWatcher = sw
-
// Start the new watcher
- if err := j.projectsWatcher.Start(ctx); err != nil {
+ if err := sw.Start(ctx); err != nil {
return err
}
+ j.mu.Lock()
+ j.projectsWatcher = sw
+ j.mu.Unlock()
+
slog.InfoContext(ctx, "Projects filesystem watcher restarted", "path", projectsDirectory)
// Perform a sync to ensure we have the latest state from the new directory