Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(daemon): respect cancellation when acquiring local directory locks (#11449)
Danila Katalshovde235b47fece
diff --git a/server/internal/daemon/local_directory.go b/server/internal/daemon/local_directory.goindex db07479b62..744bbf5b9c 100644--- a/server/internal/daemon/local_directory.go+++ b/server/internal/daemon/local_directory.go@@ -498,11 +498,8 @@ func (l *LocalPathLocker) Holder(realPath string) string {// Returns a release func that the caller must invoke (typically deferred)// to free the lock. The release is idempotent.//-// Acquire is cancellable via ctx. When ctx is cancelled while the goroutine-// is blocked on the lock, Acquire returns ctx.Err() and the lock is NOT-// taken. This is the same contract as sync.Mutex.Lock paired with-// context-aware cancellation — a daemon shutdown won't wedge inside the-// per-path wait queue.+// Acquire returns ctx.Err() without transferring ownership if cancellation+// is observed before granting the lock, including on the uncontended path.func (l *LocalPathLocker) Acquire(ctx context.Context, realPath, taskID string, onWait func(holder string)) (func(), error) {if realPath == "" {return nil, errors.New("local_directory: realpath required for lock")@@ -510,6 +507,9 @@ func (l *LocalPathLocker) Acquire(ctx context.Context, realPath, taskID string,if taskID == "" {return nil, errors.New("local_directory: taskID required for lock")}+ if err := ctx.Err(); err != nil {+ return nil, err+ }l.mu.Lock()entry, ok := l.locks[realPath]@@ -519,13 +519,21 @@ func (l *LocalPathLocker) Acquire(ctx context.Context, realPath, taskID string,}l.mu.Unlock()- // Try the fast path first — no allocation, no waiter goroutine.- if entry.mu.TryLock() {+ adopt := func() (func(), error) {+ // Both the lock and cancellation can become ready during a handoff.+ if err := ctx.Err(); err != nil {+ entry.mu.Unlock()+ return nil, err+ }entry.mu2.Lock()entry.holderID = taskIDentry.mu2.Unlock()return l.releaser(realPath, entry), nil}+ // Try the fast path first — no allocation, no waiter goroutine.+ if entry.mu.TryLock() {+ return adopt()+ }// Slow path: somebody else holds the lock. Fire onWait once with the// current holder so the daemon can stamp the server-side wait state,@@ -545,10 +553,7 @@ func (l *LocalPathLocker) Acquire(ctx context.Context, realPath, taskID string,select {case <-acquired:- entry.mu2.Lock()- entry.holderID = taskID- entry.mu2.Unlock()- return l.releaser(realPath, entry), nil+ return adopt()case <-ctx.Done():// We lost the wait — the goroutine above will still complete and// take the lock. Spin off a clean-up goroutine that releases itdiff --git a/server/internal/daemon/local_directory_test.go b/server/internal/daemon/local_directory_test.goindex 160f574740..07c70bea41 100644--- a/server/internal/daemon/local_directory_test.go+++ b/server/internal/daemon/local_directory_test.go@@ -491,6 +491,30 @@ func TestLocalPathLockerCtxCancel(t *testing.T) {}}+func TestLocalPathLockerRejectsCancelledAcquireOnFreePath(t *testing.T) {+ locker := NewLocalPathLocker()+ path := t.TempDir()+ ctx, cancel := context.WithCancel(t.Context())+ cancel()++ release, err := locker.Acquire(ctx, path, "cancelled-task", nil)+ if release != nil {+ defer release()+ t.Fatal("cancelled task acquired the lock")+ }+ if !errors.Is(err, context.Canceled) {+ t.Fatalf("acquire error = %v, want context.Canceled", err)+ }++ nextCtx, cancelNext := context.WithTimeout(t.Context(), time.Second)+ defer cancelNext()+ release, err = locker.Acquire(nextCtx, path, "next-task", nil)+ if err != nil {+ t.Fatalf("acquire for next task: %v", err)+ }+ defer release()+}+func TestLocalPathLockerDistinctPathsParallel(t *testing.T) {locker := NewLocalPathLocker()