Fix decide-after-finish refusal and run size loop data race
A run stays in r.active after finishRunState persists its terminal status until its goroutine finishes notifying, so a decision in that window was refused as "already active". decide now refuses only runs whose terminal state is not yet durable; a finished run gets ResolveDecision's already-decided / not-waiting answer, and an executing run is still refused. Tests overwrote runner.artifactsRoot after Handler/newRunner had started background goroutines (sizeLoop, session export jobs) that read it. The root is now fixed at construction through newRunnerAt, which HandlerWithOptions uses for Options.ArtifactsRoot, and tests configure it there instead of mutating the field afterwards.
This commit is contained in:
@@ -431,3 +431,63 @@ func TestHumanStageWaitingNotification(t *testing.T) {
|
||||
t.Fatalf("waiting notification receipt missing: %+v", detail.Steps[0].Notifications)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDecideOnFinishingRunIsAnsweredNotRefused pins the window between
|
||||
// finishRunState persisting a terminal status and the launch teardown removing
|
||||
// the run from r.active: a decision then gets the store's idempotent/not-waiting
|
||||
// answer, while a run that is still executing is refused as active.
|
||||
func TestDecideOnFinishingRunIsAnsweredNotRefused(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "human.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
r, err := newRunner(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer r.wg.Wait()
|
||||
r.execute = successExecute
|
||||
run := startHumanRun(t, ctx, r, store, "human-finishing")
|
||||
|
||||
// A waiting run that is genuinely executing (registered, not finished) is
|
||||
// refused.
|
||||
r.mu.Lock()
|
||||
r.active[run.ID] = activeRun{cancel: func() {}, workspace: run.Workspace}
|
||||
r.mu.Unlock()
|
||||
if _, err := r.decide(protocol.Message{RunID: run.ID, Option: "halt"}); err == nil || !strings.Contains(err.Error(), "already active") {
|
||||
t.Fatalf("decision on an executing run was not refused: %v", err)
|
||||
}
|
||||
r.mu.Lock()
|
||||
delete(r.active, run.ID)
|
||||
r.mu.Unlock()
|
||||
|
||||
if _, err := r.decide(protocol.Message{RunID: run.ID, Option: "halt", Feedback: "stop"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.wg.Wait()
|
||||
if detail, err := store.GetRun(ctx, run.ID); err != nil || detail.Status != "succeeded" {
|
||||
t.Fatalf("run did not finish: %+v %v", detail, err)
|
||||
}
|
||||
|
||||
// Simulate the finishing window: terminal status persisted, reservation not
|
||||
// yet released.
|
||||
r.mu.Lock()
|
||||
r.active[run.ID] = activeRun{cancel: func() {}, workspace: run.Workspace}
|
||||
r.mu.Unlock()
|
||||
repeat, err := r.decide(protocol.Message{RunID: run.ID, Stage: "gate", Option: "halt"})
|
||||
if err != nil {
|
||||
t.Fatalf("repeat decision on a finished run was refused: %v", err)
|
||||
}
|
||||
if repeat.Status != "succeeded" || repeat.Steps[0].Output["feedback"] != "stop" {
|
||||
t.Fatalf("repeat decision changed the run: %+v", repeat)
|
||||
}
|
||||
if _, err := r.decide(protocol.Message{RunID: run.ID, Stage: "gate", Option: "go"}); err == nil || !strings.Contains(err.Error(), "not waiting") {
|
||||
t.Fatalf("conflicting decision on a finished run did not report not waiting: %v", err)
|
||||
}
|
||||
r.mu.Lock()
|
||||
delete(r.active, run.ID)
|
||||
r.mu.Unlock()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user