From 4e2526a7af8ab1966639e7161bb6995064ce8fc8 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 6 Oct 2026 20:25:43 +0200 Subject: [PATCH] fix(reconcile): include every exceptional-state restart node, not just the last Copilot review, following up on 374079a32's own fix: serviceNodes keeps only the LAST node processed during a service's create-phase convergence -- correct for a single replica, but a scaled service can have more than one exceptional-state (paused, dead) replica, each with its own bare-restart OpStartContainer node. Using serviceNodes alone in planServiceStart's len(replicas) == 0 branch meant a downstream service_started consumer only ended up depending on the highest- numbered replica's restart, not every one of them. exceptionalStateRestartNodes collects every OpStartContainer node from containerNodes[service] (which tracks all of them, unlike serviceNodes), in deterministic replica-number order. planServiceStart now merges all of them into startChainEnds alongside the dependency prerequisites. TestPlanStart_ScaledExceptionalStateConsumerWaitsForEveryRestart pins it: two exceptional-state replicas, asserting the consumer depends on both restart nodes -- verified by ablation to depend on only the last one without the fix. Two more small, independent fixes from the same review round: - execWaitCondition's doc comment (executor_ops.go) described the pre-waitTimeout "no deadline, caller wraps ctx" design; updated to describe planExecutor.waitTimeout's actual per-node window. - waitDependency's new optional-dependency-timeout tolerance (the previous commit) gains its own direct regression test (TestWaitDependencyDeadline/expired deadline on an optional dependency is skipped, not an error) -- the existing tests covered a required timeout, user cancellation, and an optional dependency's definitive (non-timeout) failure, but not this specific path. Signed-off-by: Nicolas De Loof --- pkg/compose/executor_ops.go | 13 ++++--- pkg/compose/reconcile.go | 45 +++++++++++++++++---- pkg/compose/reconcile_start_test.go | 54 ++++++++++++++++++++++++++ pkg/compose/service_containers_test.go | 13 +++++++ 4 files changed, 111 insertions(+), 14 deletions(-) diff --git a/pkg/compose/executor_ops.go b/pkg/compose/executor_ops.go index 9c999e711..ff3791f65 100644 --- a/pkg/compose/executor_ops.go +++ b/pkg/compose/executor_ops.go @@ -219,12 +219,13 @@ func (exec *planExecutor) resolveContainerSummary(op Operation) (container.Summa // warning, not a plan failure — matching waitDependencies' own // optional-dependency handling. // -// Unlike waitDependencies, this applies no deadline of its own: nothing -// produces one yet (no ReconcileOptions field feeds a per-wait timeout the -// way api.CreateOptions.WaitTimeout does today). A future caller needing that -// — e.g. `up --wait` once it runs on the plan — wraps ctx before executing -// the plan, or adds a Timeout to the operation for execWaitCondition to wrap -// here. +// Unlike waitDependencies, this has no timeout of its own to apply here -- +// ctx is used exactly as given. upDetached (up.go) is the caller that needs +// one: planExecutor.waitTimeout gives this node its own fresh per-wait +// window, derived in executeNode's OpWaitCondition dispatch rather than +// here, the same thing start()'s own WaitTimeout already threads through +// every waitDependencies call -- without also bounding unrelated Create or +// Start work the way wrapping the whole plan's ctx would. func (exec *planExecutor) execWaitCondition(ctx context.Context, op Operation) error { s := exec.compose exec.containersMu.Lock() diff --git a/pkg/compose/reconcile.go b/pkg/compose/reconcile.go index 968f739fa..af70e484d 100644 --- a/pkg/compose/reconcile.go +++ b/pkg/compose/reconcile.go @@ -1172,6 +1172,30 @@ func (r *reconciler) setContainerNode(service string, number int, node *PlanNode r.containerNodes[service][number] = node } +// exceptionalStateRestartNodes returns every create-phase bare-restart node +// (OpStartContainer) planned for this service's exceptional-state (paused, +// dead) replicas, in deterministic replica-number order. serviceNodes keeps +// only the LAST node processed for a service's create-phase convergence -- +// correct for a single replica, but a scaled service with more than one +// exceptional-state replica needs every one of them in startChainEnds, or a +// service_started consumer could run while another replica's restart is +// still pending. +func (r *reconciler) exceptionalStateRestartNodes(service string) []*PlanNode { + planned := r.containerNodes[service] + numbers := make([]int, 0, len(planned)) + for number, node := range planned { + if node.Operation.Type == OpStartContainer { + numbers = append(numbers, number) + } + } + sort.Ints(numbers) + nodes := make([]*PlanNode, len(numbers)) + for i, number := range numbers { + nodes[i] = planned[number] + } + return nodes +} + // startPhaseReplicas collects the replicas to start, ascending number: // containers materialized by the create phase plus observed up-to-date // containers not running — exactly the isNotRunning set the imperative start @@ -1354,15 +1378,20 @@ func (r *reconciler) planServiceStart(service types.ServiceConfig) error { } if len(replicas) == 0 { // the visit still happened: dependents order after its prerequisites - // (waits, service_started edges) AND after the create phase's own - // exceptional-state restart node when it planned one (anyRunning via - // a bare OpStartContainer, same reasoning as planProviderStart) -- - // not either/or: a consumer must wait for both the dependency and - // the restart actually happening, or it can start before a - // paused/dead service's only container is running again. + // (waits, service_started edges) AND after every create phase's own + // exceptional-state restart node (anyRunning via a bare + // OpStartContainer, same reasoning as planProviderStart) -- not + // either/or: a consumer must wait for both the dependency and every + // such restart actually happening, or it can start before a + // paused/dead replica's container is running again. A scaled + // service can have more than one exceptional-state replica, so this + // collects all of them (exceptionalStateRestartNodes), not just the + // single last node serviceNodes retains. ends := depNodes - if node, ok := r.serviceNodes[service.Name]; ok && !slices.Contains(ends, node) { - ends = append(ends, node) + for _, node := range r.exceptionalStateRestartNodes(service.Name) { + if !slices.Contains(ends, node) { + ends = append(ends, node) + } } if len(ends) > 0 { r.startChainEnds[service.Name] = ends diff --git a/pkg/compose/reconcile_start_test.go b/pkg/compose/reconcile_start_test.go index 8c20f8695..82b243a60 100644 --- a/pkg/compose/reconcile_start_test.go +++ b/pkg/compose/reconcile_start_test.go @@ -490,6 +490,60 @@ func TestPlanStart_ExceptionalStateConsumerWaitsForRestart(t *testing.T) { "consumer's start must depend on app's own restart node, not just its depends_on wait:\n%s", plan) } +// TestPlanStart_ScaledExceptionalStateConsumerWaitsForEveryRestart is a +// regression test for a Copilot review finding on +// TestPlanStart_ExceptionalStateConsumerWaitsForRestart's own fix: for a +// scaled service, serviceNodes keeps only the LAST create-phase node +// processed, so a consumer's start only ended up depending on the highest- +// numbered replica's restart. With two exceptional-state replicas, the +// consumer must depend on BOTH restart nodes, not just one. +func TestPlanStart_ScaledExceptionalStateConsumerWaitsForEveryRestart(t *testing.T) { + two := 2 + app := types.ServiceConfig{Name: "app", Deploy: &types.DeployConfig{Replicas: &two}} + consumer := serviceWithDeps("consumer", types.DependsOnConfig{"app": {Condition: types.ServiceConditionStarted, Required: true}}) + project := &types.Project{ + Name: "myproject", + Services: types.Services{"app": app, "consumer": consumer}, + } + appHash, err := serviceHashWithResolvedRefs(app, nil) + assert.NilError(t, err) + consumerHash, err := serviceHashWithResolvedRefs(consumer, nil) + assert.NilError(t, err) + observed := emptyObserved() + observed.Containers["app"] = []ObservedContainer{ + observedServiceContainer("app", 1, container.StatePaused, appHash), + observedServiceContainer("app", 2, container.StateDead, appHash), + } + observed.Containers["consumer"] = []ObservedContainer{ + observedServiceContainer("consumer", 1, container.StateExited, consumerHash), + } + + plan, err := reconcile(t.Context(), project, observed, startScopeOptions(ScopeCreateStart), noPrompt) + assert.NilError(t, err) + + var restart1, restart2, consumerStartNode *PlanNode + for _, n := range plan.Nodes { + switch { + case n.Operation.Type == OpStartContainer && n.Operation.ResourceID == "service:app:1": + restart1 = n + case n.Operation.Type == OpStartContainer && n.Operation.ResourceID == "service:app:2": + restart2 = n + case n.Operation.Type == OpStartContainer && n.Operation.ResourceID == "service:consumer:1": + consumerStartNode = n + } + } + if restart1 == nil || restart2 == nil { + t.Fatalf("expected both replicas' bare-restart nodes:\n%s", plan) + } + if consumerStartNode == nil { + t.Fatalf("expected a start node for consumer:\n%s", plan) + } + assert.Assert(t, slices.Contains(consumerStartNode.DependsOn, restart1), + "consumer's start must depend on replica 1's restart node:\n%s", plan) + assert.Assert(t, slices.Contains(consumerStartNode.DependsOn, restart2), + "consumer's start must depend on replica 2's restart node too, not just the last one processed:\n%s", plan) +} + // A replica condemned by scale-down must never receive a start-phase node: // the imperative engine only starts what survives the convergence — and the // condemned replica does not count as running for the pre_start gating. diff --git a/pkg/compose/service_containers_test.go b/pkg/compose/service_containers_test.go index b925ce845..148f60f95 100644 --- a/pkg/compose/service_containers_test.go +++ b/pkg/compose/service_containers_test.go @@ -865,6 +865,19 @@ func TestWaitDependencyDeadline(t *testing.T) { assert.Error(t, err, "timeout waiting for dependencies") }) + // Regression guard: an optional dependency's deadline expiring (new as of + // planExecutor.waitTimeout giving execWaitCondition a real timeout for + // the first time -- see waitDependency's ctx.Done() branch) must be + // tolerated exactly like any other definitive failure of an optional + // dependency, not surfaced as "timeout waiting for dependencies". + t.Run("expired deadline on an optional dependency is skipped, not an error", func(t *testing.T) { + optionalDeps := types.DependsOnConfig{ + "db": {Condition: types.ServiceConditionHealthy, Required: false}, + } + err := tested.(*composeService).waitDependencies(t.Context(), &project, "app", optionalDeps, containers, 50*time.Millisecond) + assert.NilError(t, err) + }) + t.Run("user cancellation is not a wait failure", func(t *testing.T) { ctx, cancel := context.WithCancel(t.Context()) cancel()