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:
2026-11-01 18:58:14 +00:00
parent c41f5caa4f
commit 3bfb3d6b44
10 changed files with 89 additions and 22 deletions
+20 -2
View File
@@ -87,6 +87,15 @@ type runner struct {
}
func newRunner(ctx context.Context, s *pipeline.Store) (*runner, error) {
return newRunnerAt(ctx, s, "")
}
// newRunnerAt builds a runner whose stage artifacts live under artifactsRoot
// ("" means <store directory>/artifacts). artifactsRoot is fixed before any
// background goroutine (session export jobs, notifications, the size backfill)
// starts and is never written afterwards, so those goroutines read it without
// locking.
func newRunnerAt(ctx context.Context, s *pipeline.Store, artifactsRoot string) (*runner, error) {
if err := s.AcquireRunnerLock(); err != nil {
return nil, err
}
@@ -123,7 +132,10 @@ func newRunner(ctx context.Context, s *pipeline.Store) (*runner, error) {
if err := s.RecoverRuns(ctx); err != nil {
return nil, err
}
root := filepath.Join(s.Directory(), "artifacts")
root := artifactsRoot
if root == "" {
root = filepath.Join(s.Directory(), "artifacts")
}
active := map[int64]activeRun{}
rows, err := s.ListRuns(ctx)
if err != nil {
@@ -780,7 +792,13 @@ func (r *runner) decide(request protocol.Message) (*protocol.RunDetail, error) {
r.mu.Unlock()
return nil, err
}
if _, busy := r.active[detail.ID]; busy {
// finishRunState persists the terminal status under r.mu, but the run stays
// in r.active until its goroutine finishes notifying and the launch teardown
// runs. A run whose terminal state is already durable (FinishedAt is only set
// once cleanup is confirmed) has no executor left to race with a decision, so
// ResolveDecision answers it ("already decided" or "not waiting") and never
// resumes a terminal run. A run that is still executing is refused.
if _, busy := r.active[detail.ID]; busy && detail.FinishedAt == "" {
r.mu.Unlock()
return nil, fmt.Errorf("run %d is already active; wait for it to pause before deciding", detail.ID)
}