Skip to content

cancel_task leaves the driver thread and BRC event loop running: a cancelled pipeline keeps spawning agents #3633

Closed

Description

@jwbron

Summary

cancel_task sets the pipeline status to CANCELLED, tears down its containers, and clears its
runtime state — but it never stops the _run_pipeline driver thread or the BRC event loop. Both
keep running in-process, so a cancelled pipeline keeps driving its slice DAG: admitting the next
slice, creating its integration branch, and spawning agents.

Observed on issue-3596-v2, which spawned slice-3 agents two hours and seven minutes after it
was cancelled
.

Related but distinct from #3632. That one is "cancel destroys the state it promised to preserve";
this one is "cancel does not stop the work it promised to stop." They are opposite halves of the
same broken contract and probably want fixing together.

Mechanism

The cancel path is update_pipeline → status CANCELLED (routes/pipelines/_routes_crud.py). It
does exactly three things:

  1. spawns _background_cleanup, which calls spawner.cleanup_pipeline(...) to remove containers;
  2. calls _clear_pipeline_runtime_state (tracker + message store — see cancel_task(cleanup=false) destroys the consensus + message state its own contract promises to preserve #3632);
  3. writes the new status.

It never calls stop_event_loop, and it never signals the driver thread. The only callers of
stop_event_loop in the tree are in routes/pipelines/_run_concurrent.py (lines 486, 567, 1144) —
i.e. the concurrent phase stops its own loop when the phase function returns normally. A cancel
from outside does not make it return.

So killing the pods removes the symptom while the thing that spawns pods keeps running. The next
poll re-derives its arms and spawns again.

Note the assumption this violates, already written down in _routes_restart.py:314:

Unlike a RUNNING pipeline — whose live event loop picks up the consensus reset below and respawns
within one poll — a FAILED/CANCELLED pipeline has NO live event loop: _run_concurrent_phase
already returned and stop_event_loop() tore the loop down on its way out, and the
_run_pipeline driver thread has exited.

The restart path's correctness argument depends on that being true. It is not true for a pipeline
cancelled mid-phase.

Evidence

issue-3596-v2 was cancelled at 20:48:52Z via cancel_task(cleanup=false). Branch state
captured immediately after the cancel — there is no slice-3:

origin/egg/issue-3596-v2/slice-1  tip=66d99b53c  39 commits ahead of main
origin/egg/issue-3596-v2/slice-2  tip=4f6217292  33 commits ahead of main
origin/egg/issue-3596-v2/work     tip=94057299c  60 commits ahead of main

At 22:55:35Z, with the pipeline still persisted as CANCELLED, two agent pods appeared:

egg-sandbox-egg-agent-issue-3596-v2-slice-3-coder-104ee0c848hdt        0/1  Completed
egg-sandbox-egg-agent-issue-3596-v2-slice-3-documenter-754wb9s7       0/1  Completed

They were live enough to call home — the orchestrator logged their traffic at 22:56:50Z:

POST /api/v1/pipelines/issue-3596-v2/heartbeat              status=200
POST /api/v1/pipelines/issue-3596-v2/consensus/next-action  status=200

And a slice-3 integration branch now exists, forked from slice-1's tip:

origin/egg/issue-3596-v2/slice-3  tip=66d99b53c  39 commits ahead of main

Pipeline status throughout, and still now:

status: cancelled   phase: implement   updated: 2026-07-25T22:55:25Z   run_epoch: 2026-07-25T04:50:56Z

It was not startup reconciliation. startup_reconciliation.py:133 and :305 both skip any
pipeline whose status is not RUNNING, and the replacement orchestrator pod's log contains no
non-API mention of issue-3596-v2 at all. Kubernetes events show a single pod transition (old pod
killed, replacement created ~30s later), so the spawn came from the original orchestrator process
that had been running since before the cancel.

Impact

Bounded in this instance, but only by luck of timing — the agents were killed by the redeploy about
a minute in:

  • Two agent pods spawned and ran against a cancelled pipeline (real model spend).
  • One integration branch created and pushed.
  • No commits, no PRs.

The general case is worse. A cancelled pipeline that is not about to be redeployed keeps its loop
indefinitely, so it can walk the entire remaining slice DAG: spawn agents, let them commit, push
branches, and open slice PRs — all for a run the operator believes is stopped. Given auto_create_pr
defaults on, "cancelled" pipelines opening PRs is reachable today.

It also corrupts anything that counts operator interventions or agent invocations for a run, which
is exactly what #3601's evaluation is measuring.

What I could not establish

Why the spawn happened at 22:55 rather than shortly after the 20:48 cancel. Something let the slice
scheduler admit slice-3 at that moment — plausibly slice-2's agents being torn down by the redeploy
freeing the concurrency slot, or a poll tick after a park expired — but the original orchestrator
pod's log died with the pod, so this is unverified. The two-hour gap means a naive repro ("cancel,
then watch for a minute") may show nothing; the loop is alive the whole time regardless.

Suggested fix

Make cancel stop the driver. Concretely:

  1. Signal the _run_pipeline driver thread to exit on a terminal status transition (a per-pipeline
    stop event checked at the phase loop's poll points), and call stop_event_loop for the pipeline
    from the cancel path rather than relying on _run_concurrent_phase returning.
  2. Have the event loop and slice scheduler re-read pipeline status and refuse to spawn when it is
    terminal — defence in depth, so a missed stop signal costs a no-op poll rather than a slice.
  3. Order it so the loop stops before _background_cleanup removes containers; today the cleanup
    races a live loop that is entitled to spawn replacements.
  4. Add a regression test: cancel a pipeline mid-implement with an un-admitted slice remaining, then
    assert no Jobs are created and no new branches appear on subsequent poll intervals.

Activity

  1. added
    p0
    on Jul 26, 2026
  2. jwbron commented on Jul 26, 2026

    @jwbron
    OwnerAuthor

    Third observation, on issue-3632-v1 at cancel time (2026-07-26 21:05Z).

    cancel_task(cleanup=false) returned and the pipeline persisted as cancelled. Within the next minute the event loop spawned two more agent Jobs:

    egg-sandbox-egg-agent-issue-3632-v1-coder-ace18ef0        Complete  1/1   5s
    egg-sandbox-egg-agent-issue-3632-v1-documenter-e3688662   Complete  1/1   4s
    

    Both exited in ~5 seconds, so the blast radius here was trivial — but the mechanism is the same one that walked issue-3596-v2 onto slice-3 two hours after its cancel: the driver and event loop are not stopped, so the next poll derives arms and spawns.

    Two things this adds to the report:

    1. It is reproducible on demand, not a rare race. Three observations across two pipelines.
    2. The short runtime is luck, not containment. These two exited quickly because the pods read a cancelled pipeline and stopped. Nothing prevented the spawn, and an agent that gets further before noticing (as slice-3's did) will commit, push, and — with auto_create_pr on — can open a PR from a pipeline the operator believes is stopped.
  3. added a commit that references this issue on Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    p0

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions