watch: data race between Daemon.Stop and lazy dependency-worker start
Found while writing the #201 burst tests on PR #215. Pre-existing on main; the PR does not change it.
Reproduce
A daemon whose event loop runs the real refreshDependencies (every existing test that drives eventLoop stubs daemonRefreshDependencies, which is why CI has never shown this), then Stop() while the first graph invalidation is being handled:
go test ./watch/ -race -run TestStopRacesLazyWorkerStart -count=20
with this test body (not committed):
d, _ := NewDaemon(root, false) // root holds one a.go
d.watcher.Close()
d.watcher.Events = make(chan fsnotify.Event, 8)
d.watcher.Errors = make(chan error)
os.MkdirAll(d.runtimeDir, 0o755)
daemonComputeTopology = func(*Daemon) {}
d.eventLoopWG.Add(1)
go func() { defer d.eventLoopWG.Done(); d.eventLoop() }()
d.watcher.Events <- fsnotify.Event{Name: filepath.Join(root, "a.go"), Op: fsnotify.Create}
time.Sleep(time.Millisecond)
d.Stop()
Race output (trimmed to the two stacks)
WARNING: DATA RACE
Write at 0x00c0001562a8 by goroutine 8:
codemap/watch.(*Daemon).refreshDependencies.(*Daemon).startDependencyWorker.func1()
watch/daemon.go:470
sync.(*Once).doSlow()
sync.(*Once).Do()
codemap/watch.(*Daemon).startDependencyWorker()
watch/daemon.go:462
codemap/watch.(*Daemon).refreshDependencies()
watch/daemon.go:437
codemap/watch.(*Daemon).eventLoop.func1()
watch/events.go:231
codemap/watch.(*Daemon).eventLoop()
watch/events.go:447
Previous read at 0x00c0001562a8 by goroutine 6:
codemap/watch.(*Daemon).Stop()
watch/daemon.go:276
codemap/watch.TestStopRacesLazyWorkerStart()
What is unsynchronised
Daemon.dependencyCancel (watch/daemon.go:470, written inside dependencyOnce.Do on the event-loop goroutine) is read by Stop() at watch/daemon.go:276 on the caller's goroutine with no lock, channel or Once between them. Stop closes d.done first, but startDependencyWorker does not observe done, so a loop iteration already inside refreshDependencies still runs the Once after Stop has read the nil cancel. The same window covers dependencyWorkerWG.Add(1) (daemon.go:472) racing dependencyWorkerWG.Wait() (daemon.go:278), which the race detector reports less often but which is the more dangerous half: a worker started after Wait returned outlives Stop, keeps a goroutine and a context alive, and can publish a graph result into a daemon that has already closed its watcher.
Why #215 stubs around it
PR #215 adds burst tests that drive eventLoop with the real daemon; its first run under -race hit exactly this report. The fix belongs to the daemon lifecycle, not to the throttle change, and every existing event-loop test already stubs daemonRefreshDependencies for the same reason, so #215 follows that convention and leaves the lifecycle to this issue.
Proposed fix
Start the worker eagerly instead of lazily: create dependencyRequests, dependencyResults, the cancellable context and the worker goroutine in NewDaemon (or at the top of Start, before the event loop goroutine exists), and delete dependencyOnce and startDependencyWorker. Then dependencyCancel and the WaitGroup are written once, before any concurrent reader exists, and Stop reads them after eventLoopWG.Wait() with a happens-before edge through done. The worker already blocks on dependencyRequests and ctx.Done(), so an idle worker costs one parked goroutine. If lazy start must stay (large repos skip the graph entirely), the alternative is to guard both startDependencyWorker and the first half of Stop with one mutex and have startDependencyWorker return early when done is closed; the eager version is the smaller diff and removes the Once.
Refs #155 (the daemon has no single owner for its lifecycle; this is one concrete consequence).
🤖 Generated with Claude Code
https://claude.ai/code/session_01NRaBgKRAZMX9hjbkkefA7T