From 0dde93e57e9271a2522f5503161db3ab4fbd0343 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 13 Oct 2026 16:22:40 +1100 Subject: [PATCH] fix(taskactions): a failed callee cancel no longer leaves the caller's run going Issue: archie-core-kig5j --- internal/domain/taskactions/service.go | 23 ++++++++++--- internal/domain/taskactions/service_test.go | 37 +++++++++++++++------ 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/internal/domain/taskactions/service.go b/internal/domain/taskactions/service.go index 64f02aeab..0a6515cb8 100644 --- a/internal/domain/taskactions/service.go +++ b/internal/domain/taskactions/service.go @@ -256,7 +256,7 @@ func (s Service) Withdraw(ctx context.Context, id int64, reason string) error { event := s.attributed(task, Actor{}) event.Kind, event.Detail = events.KindTaskWithdrawn, "issue withdrawn: "+reason event.Data = map[string]any{"reason": reason} - callees, err := s.Store.CancelExecution(ctx, task.ID, event.Detail, declined) + callees, err := s.cancelExecution(ctx, task.ID, event.Detail, declined) if err != nil { return err } @@ -297,7 +297,7 @@ func (s Service) apply(ctx context.Context, task *Task, actor Actor, action task // A rejection ends the run's work wherever it sits, so it rides the // one cancel path like every other operator stop. o.event.Kind, o.event.Detail, o.verb = rejectedKind(actor), actor.describe("rejected"), "declined" - callees, err := s.Store.CancelExecution(ctx, task.ID, actor.describe("rejected"), declined) + callees, err := s.cancelExecution(ctx, task.ID, actor.describe("rejected"), declined) if err != nil { return o, err } @@ -388,6 +388,19 @@ func selectedFindingKeys(gate workflowtask.ReviewGate, requested []string) ([]st // deliver cancels the in-memory contexts of runs the store has already // recorded as cancelled. +// cancelExecution cancels the task's execution. The store returns the callees +// it reached together with an error when only their cancels failed; the +// caller's own cancel committed then, so the failure is reported and the +// action goes on to stop the run. +func (s Service) cancelExecution(ctx context.Context, taskID int64, reason, to string) ([]int64, error) { + callees, err := s.Store.CancelExecution(ctx, taskID, reason, to) + if err != nil && len(callees) > 0 { + s.warn("callee cancel failed; the caller's run is stopped", "task", taskID, "err", err) + return callees, nil + } + return callees, err +} + func (s Service) deliver(taskIDs ...int64) { if s.CancelTask == nil { return @@ -507,7 +520,7 @@ func (s Service) applyStop(ctx context.Context, task *Task, actor Actor, o outco // context cancel that found nothing executing says so; the parked // execution is recorded either way, and the task's own unwind cannot win // the race any more -- its next step write is already stale. - callees, err := s.Store.CancelExecution(ctx, task.ID, o.event.Detail, parked) + callees, err := s.cancelExecution(ctx, task.ID, o.event.Detail, parked) if err != nil { return o, err } @@ -523,7 +536,7 @@ func (s Service) applyCancelOrAbandon(ctx context.Context, task *Task, action ta // Abandoning ends archie's run, not the issue: no verb, so the issue // stays open for a human. o.event.Kind, o.event.Detail = events.KindTaskAbandoned, actor.describe("abandoned") - callees, err := s.Store.CancelExecution(ctx, task.ID, o.event.Detail, declined) + callees, err := s.cancelExecution(ctx, task.ID, o.event.Detail, declined) if err != nil { return o, err } @@ -531,7 +544,7 @@ func (s Service) applyCancelOrAbandon(ctx context.Context, task *Task, action ta return o, nil } o.event.Kind, o.event.Detail, o.verb = events.KindTaskCancelled, actor.describe("cancelled"), "cancelled" - callees, err := s.Store.CancelExecution(ctx, task.ID, o.event.Detail, declined) + callees, err := s.cancelExecution(ctx, task.ID, o.event.Detail, declined) if err != nil { return o, err } diff --git a/internal/domain/taskactions/service_test.go b/internal/domain/taskactions/service_test.go index be0ab0c8b..fd635b064 100644 --- a/internal/domain/taskactions/service_test.go +++ b/internal/domain/taskactions/service_test.go @@ -2,6 +2,7 @@ package taskactions import ( "context" + "errors" "slices" "testing" @@ -14,6 +15,9 @@ type cancelStore struct { Store task Task callees []int64 + // failure is returned with the callees, as the store does when only their + // cancels failed. + failure error } func (f *cancelStore) TaskByID(context.Context, int64) (*Task, error) { @@ -22,7 +26,7 @@ func (f *cancelStore) TaskByID(context.Context, int64) (*Task, error) { } func (f *cancelStore) CancelExecution(context.Context, int64, string, string) ([]int64, error) { - return f.callees, nil + return f.callees, f.failure } func (f *cancelStore) InsertEvent(context.Context, events.Event) (int64, error) { return 1, nil } @@ -30,20 +34,27 @@ func (f *cancelStore) InsertEvent(context.Context, events.Event) (int64, error) // Every operator action that cancels a run must also stop the runs of the // callees the store cancelled with it, or they run on to completion. func TestCancellingActionsStopCallees(t *testing.T) { + failure := errors.New("cancel callee 7: boom") tests := []struct { - status string - action taskstate.Action + name string + status string + action taskstate.Action + failure error + // parked is a task with no live run of its own to stop. + parked bool }{ - {status: taskstate.Queued, action: taskstate.ActionCancel}, - {status: taskstate.Running, action: taskstate.ActionStop}, - {status: taskstate.Running, action: taskstate.ActionReject}, - {status: taskstate.Parked, action: taskstate.ActionAbandon}, + {name: "cancel", status: taskstate.Queued, action: taskstate.ActionCancel}, + {name: "stop", status: taskstate.Running, action: taskstate.ActionStop}, + {name: "reject", status: taskstate.Running, action: taskstate.ActionReject}, + {name: "abandon", status: taskstate.Parked, action: taskstate.ActionAbandon, parked: true}, + {name: "stop with a failed callee cancel", status: taskstate.Running, action: taskstate.ActionStop, failure: failure}, + {name: "cancel with a failed callee cancel", status: taskstate.Queued, action: taskstate.ActionCancel, failure: failure}, } for _, tt := range tests { - t.Run(string(tt.action), func(t *testing.T) { + t.Run(tt.name, func(t *testing.T) { var stopped []int64 s := Service{ - Store: &cancelStore{task: Task{ID: 1, Status: tt.status}, callees: []int64{7, 9}}, + Store: &cancelStore{task: Task{ID: 1, Status: tt.status}, callees: []int64{7, 9}, failure: tt.failure}, CancelTask: func(id int64) bool { stopped = append(stopped, id) return true @@ -52,9 +63,13 @@ func TestCancellingActionsStopCallees(t *testing.T) { if err := s.Apply(t.Context(), nil, Actor{}, 1, tt.action, ActionPayload{}); err != nil { t.Fatal(err) } - for _, callee := range []int64{7, 9} { + want := []int64{7, 9} + if !tt.parked { + want = append(want, 1) + } + for _, callee := range want { if !slices.Contains(stopped, callee) { - t.Fatalf("stopped %v, want callee %d among them", stopped, callee) + t.Fatalf("stopped %v, want %d among them", stopped, callee) } } })