Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(daemon): drain background repo syncs before test teardown (#2026)
Bohan Jiangc4352da126a0
diff --git a/server/internal/daemon/daemon.go b/server/internal/daemon/daemon.goindex 1f8128f213..99c6b6682f 100644--- a/server/internal/daemon/daemon.go+++ b/server/internal/daemon/daemon.go@@ -69,6 +69,13 @@ type Daemon struct {activeEnvRootsMu sync.MutexactiveEnvRoots map[string]int // env root path -> reference count (handles reuse paths marked twice)++ // bgSyncs tracks background goroutines started by registerTaskRepos so+ // callers (notably tests using t.TempDir-backed cache roots) can wait for+ // them to drain before tearing the daemon down. Without this the bg+ // goroutine can race against t.TempDir cleanup, leaving a partially+ // deleted bare clone and an unrelated `not empty` cleanup failure.+ bgSyncs sync.WaitGroup}// New creates a new Daemon instance.@@ -454,10 +461,23 @@ func (d *Daemon) registerTaskRepos(workspaceID string, repos []RepoData) {// `ensureRepoReady` reports a meaningful error if the cache isn't ready// yet, so the agent's first checkout will surface a sync failure// without silently treating it as a config bug.- go d.syncWorkspaceRepos(workspaceID, toSync)+ d.bgSyncs.Add(1)+ go func() {+ defer d.bgSyncs.Done()+ d.syncWorkspaceRepos(workspaceID, toSync)+ }()}}+// waitBackgroundSyncs blocks until every background sync started by+// registerTaskRepos has finished. Intended for test teardown: tests that+// hand the daemon a t.TempDir-backed repo cache must call this before+// returning, otherwise an in-flight clone/fetch can race against TempDir+// cleanup and surface as an unrelated "directory not empty" failure.+func (d *Daemon) waitBackgroundSyncs() {+ d.bgSyncs.Wait()+}+func (d *Daemon) syncWorkspaceRepos(workspaceID string, repos []RepoData) {if d.repoCache == nil {returndiff --git a/server/internal/daemon/daemon_test.go b/server/internal/daemon/daemon_test.goindex b8f451c6ca..85ef99fc95 100644--- a/server/internal/daemon/daemon_test.go+++ b/server/internal/daemon/daemon_test.go@@ -368,13 +368,19 @@ func newRepoReadyTestDaemon(t *testing.T, handler http.HandlerFunc) *Daemon {t.Helper()srv := httptest.NewServer(handler)t.Cleanup(srv.Close)- return &Daemon{+ d := &Daemon{client: NewClient(srv.URL),repoCache: repocache.New(t.TempDir(), slog.Default()),logger: slog.Default(),workspaces: make(map[string]*workspaceState),runtimeIndex: make(map[string]Runtime),}+ // Drain background syncs (started by registerTaskRepos) before the+ // t.TempDir cache root is cleaned up, otherwise an in-flight clone/fetch+ // races against the deletion and the test fails with a misleading+ // "directory not empty" cleanup error.+ t.Cleanup(d.waitBackgroundSyncs)+ return d}func TestExecuteAndDrain_ResumeFailureFallback(t *testing.T) {