diff --git a/pkgs/mcpx/internal/tasks/tasks.go b/pkgs/mcpx/internal/tasks/tasks.go index afba5965a5..863e9c5f1a 100644 --- a/pkgs/mcpx/internal/tasks/tasks.go +++ b/pkgs/mcpx/internal/tasks/tasks.go @@ -69,7 +69,11 @@ type Task struct { fault *Fault cancel context.CancelFunc done chan struct{} - timer *time.Timer + // doneClosed, guarded by Store.mu, makes closing done once-only: + // grace expiry closes it without the work's return, and the worker + // goroutine's close after finish must not close it a second time. + doneClosed bool + timer *time.Timer // cancelTimer bounds the in-memory cooperative-cancellation window. The // cancelling mark and timer are deliberately not persisted or exposed. cancelTimer *time.Timer @@ -455,6 +459,7 @@ FROM tasks`) } close(t.done) + t.doneClosed = true s.tasks[t.TaskID] = t t.refreshTTLRemaining(now) s.armExpiryLocked(t, time.Duration(expiresAtMs-nowMs)*time.Millisecond) @@ -901,7 +906,7 @@ func (s *Store) StartWithDeadline(requestor Requestor, ttl int64, deadline time. go func() { result, fault := fn(ctx) moved := s.finish(t, result, fault) - close(t.done) + s.closeDone(t) if moved { s.changed(t.TaskID) } @@ -1203,6 +1208,25 @@ func (s *Store) CancelImmediate(id string, reason string) (Task, bool) { return snap, true } +// closeDoneLocked releases Result waiters exactly once. The caller +// holds s.mu. Terminal transitions recorded without the work's return +// (grace expiry) close done themselves; the worker goroutine's close +// after finish is then a no-op instead of a double-close panic. +func (s *Store) closeDoneLocked(t *Task) { + if t.doneClosed { + return + } + t.doneClosed = true + close(t.done) +} + +// closeDone is closeDoneLocked for callers not holding s.mu. +func (s *Store) closeDone(t *Task) { + s.mu.Lock() + defer s.mu.Unlock() + s.closeDoneLocked(t) +} + // expireCancellation records the cancel only if the task's work did not // record a terminal status first. Called by the per-task grace timer. func (s *Store) expireCancellation(t *Task, reason string) { @@ -1219,6 +1243,10 @@ func (s *Store) expireCancellation(t *Task, reason string) { } s.armRetentionLocked(t) s.saveTaskLocked(t) + // The work may never return (that is the path grace expiry exists + // for), so release Result waiters here: they read the recorded + // Cancelled status and return the cancelled fault (#6897). + s.closeDoneLocked(t) s.mu.Unlock() s.logTerminal(t) s.changed(t.TaskID) diff --git a/pkgs/mcpx/internal/tasks/tasks_test.go b/pkgs/mcpx/internal/tasks/tasks_test.go index 786e091fd2..7d36c323dd 100644 --- a/pkgs/mcpx/internal/tasks/tasks_test.go +++ b/pkgs/mcpx/internal/tasks/tasks_test.go @@ -821,6 +821,41 @@ func TestCancelGraceExpiryWinsOverLateResult(t *testing.T) { } } +func TestCancelGraceExpiryReleasesResultWaiters(t *testing.T) { + // Regression for #6897: grace expiry records Cancelled, so Result() + // waiters must be released with the cancelled fault even when the + // work itself never returns. Before the fix, expireCancellation + // never closed done, so this Result call hung until its own + // context expired; releasing the work afterwards also exercises + // the once-only close (the late worker return must not re-close). + st := tasks.New() + st.CancelGrace = 15 * time.Millisecond + release := make(chan struct{}) + task, err := st.Start(tasks.Caller("tester"), 0, func(ctx context.Context) (any, *tasks.Fault) { + <-release + return "late result", nil + }) + if err != nil { + t.Fatal(err) + } + defer close(release) + if _, ok := st.CancelWithReason(task.TaskID, "grace expiry releases waiters"); !ok { + t.Fatal("cancel lost the task") + } + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + _, fault, err := st.Result(ctx, task.TaskID) + if err != nil { + t.Fatalf("grace expiry did not release the Result waiter: %v", err) + } + if fault == nil || fault.Message != "the task was cancelled" { + t.Fatalf("waiter should get the cancelled fault, got %v", fault) + } + if got, _ := st.Get(task.TaskID); got.Status != tasks.Cancelled { + t.Fatalf("status = %q, want cancelled", got.Status) + } +} + func TestFaultWithinCancelGraceKeepsItsFailure(t *testing.T) { st := tasks.New() st.CancelGrace = time.Second