Dispatch ↔ Deploy FIFO cross-wait deadlock: dispatches block deploys, deploys block dispatches #8

Open
opened 2026-04-18 19:49:23 -07:00 by nelsong6 · 1 comment
nelsong6 commented 2026-04-18 19:49:23 -07:00 (Migrated from github.com)

Observed today

In the api repo, 3 dispatches and 2 deploys were all stuck for 30+ minutes — every one of them stalled in its wait-step, none making progress. Had to kill all of them and re-run the chain manually.

State at the time (run ids are api/main):

Run Workflow Sha Blocking step Age
24618743074 Dependency Dispatch 0f8bffa Wait for Queue Position 30m
24619076957 Dependency Dispatch fcdc9fc Wait for Queue Position 15m
24619079991 Dependency Dispatch fcdc9fc Wait for Queue Position 15m
24618751886 Deploy 0f8bffa Wait for Dispatches 25m
24619094145 Deploy fcdc9fc Wait for Dispatches 15m

Why it deadlocks

Each side's FIFO guard counts the other side as blocking, symmetrically:

  • dispatch.yml / fifo-wait:
    EARLIER=count(dispatches with databaseId < current && status=in_progress)
    DEPLOYS=count(deploys with status=in_progress)          # any deploy
    TOTAL=EARLIER+DEPLOYS; wait until 0
    
  • deploy.yml / wait-for-dispatches:
    DISPATCHES=count(dispatches with status=in_progress)    # any dispatch
    wait until 0
    

Both sides treat the other as "anything in progress blocks me". Neither uses ordering. So once a deploy and a dispatch are both in progress, each one's guard stays true forever.

Minimal repro

  1. Push a host repo → its publish-routes workflow publishes a package and fires repository_dispatch at the api.
  2. Before that dispatch finishes, push the api directly (e.g. a routes-mount change). Phase 1 CI Build completes → workflow_run triggers the Deploy workflow.
  3. While the first deploy is in Wait for Dispatches, a second host publishes a package → new dispatch fires.
  4. From here on nothing can move: dispatches see "deploy in progress", deploys see "dispatches in progress".

Any concurrent dispatch+deploy at the same time hits this — doesn't even need a third event; the first two are enough if neither clears on its own.

Fix options

databaseId is a monotonic global counter across GH Actions runs in a repo, so the ordering is total. Change the deploy guard to:

DISPATCHES=$(gh run list --workflow=dispatch.yml --status=in_progress \
  --json databaseId --jq \"[.[] | select(.databaseId < ${{ github.run_id }})] | length\")

Now a deploy only waits on dispatches that started before it; dispatches still wait on earlier dispatches + all deploys. The cycle can't form because at most one side blocks the other in any given temporal order.

Counter-check: if the deploy runs first and a dispatch starts later, the deploy doesn't wait, finishes, and the dispatch proceeds. If the dispatch starts first, the deploy waits, the dispatch finishes, and the deploy proceeds. Either way acyclic.

Pros: one-line change, preserves intent (deploys serialize behind ongoing dispatches).
Cons: a deploy that started a second before a dispatch will jump ahead of it — but that matches real-world ordering already, since the deploy's underlying Phase 1 build preceded the dispatch trigger anyway.

2. Single queue via GH Actions concurrency groups

Replace both custom wait loops with concurrency: group: api-deploy, cancel-in-progress: false. The downside: cancel-in-progress=false queues serially but doesn't give any visibility; cancel-in-progress=true would kill running work. And deploys-triggered-by-workflow_run and dispatches would need the same group key to actually serialize together.

3. Fold deploy.yml into dispatch.yml

Have the workflow_run-triggered path call the same job chain via workflow_call. One queue, one wait. Bigger change but removes the duplication entirely.

Recommendation

Option 1. One-line edit to deploy.yml (and whichever shared template hosts it). Consider also changing the dispatch's DEPLOYS count to use the same ordering — today it blocks on any deploy even if the deploy started after it, which is correct today but worth auditing.

Notes

  • Related but distinct from #7 (direct-push race with humans). This one is bot-vs-bot, no human involvement needed.
  • Recovery procedure when this fires: cancel all stuck runs, manually bump api/package.json + lockfile to the latest published versions (the stuck dispatches were trying to do this but never got past their wait step), push a commit, let the single fresh Phase 1 build → Deploy chain run alone.
## Observed today In the `api` repo, 3 dispatches and 2 deploys were all stuck for 30+ minutes — every one of them stalled in its wait-step, none making progress. Had to kill all of them and re-run the chain manually. State at the time (run ids are api/main): | Run | Workflow | Sha | Blocking step | Age | |---|---|---|---|---| | 24618743074 | Dependency Dispatch | 0f8bffa | Wait for Queue Position | 30m | | 24619076957 | Dependency Dispatch | fcdc9fc | Wait for Queue Position | 15m | | 24619079991 | Dependency Dispatch | fcdc9fc | Wait for Queue Position | 15m | | 24618751886 | Deploy | 0f8bffa | Wait for Dispatches | 25m | | 24619094145 | Deploy | fcdc9fc | Wait for Dispatches | 15m | ## Why it deadlocks Each side's FIFO guard counts the other side as blocking, symmetrically: - `dispatch.yml` / `fifo-wait`: ``` EARLIER=count(dispatches with databaseId < current && status=in_progress) DEPLOYS=count(deploys with status=in_progress) # any deploy TOTAL=EARLIER+DEPLOYS; wait until 0 ``` - `deploy.yml` / `wait-for-dispatches`: ``` DISPATCHES=count(dispatches with status=in_progress) # any dispatch wait until 0 ``` Both sides treat the other as "anything in progress blocks me". Neither uses ordering. So once a deploy and a dispatch are both in progress, each one's guard stays true forever. ## Minimal repro 1. Push a host repo → its publish-routes workflow publishes a package and fires `repository_dispatch` at the api. 2. Before that dispatch finishes, push the api directly (e.g. a routes-mount change). Phase 1 CI Build completes → `workflow_run` triggers the Deploy workflow. 3. While the first deploy is in `Wait for Dispatches`, a second host publishes a package → new dispatch fires. 4. From here on nothing can move: dispatches see "deploy in progress", deploys see "dispatches in progress". Any concurrent dispatch+deploy at the same time hits this — doesn't even need a third event; the first two are enough if neither clears on its own. ## Fix options ### 1. Symmetric ordering: deploys only wait for dispatches with an earlier `databaseId` *(recommended)* `databaseId` is a monotonic global counter across GH Actions runs in a repo, so the ordering is total. Change the deploy guard to: ``` DISPATCHES=$(gh run list --workflow=dispatch.yml --status=in_progress \ --json databaseId --jq \"[.[] | select(.databaseId < ${{ github.run_id }})] | length\") ``` Now a deploy only waits on dispatches that started *before* it; dispatches still wait on earlier dispatches + all deploys. The cycle can't form because at most one side blocks the other in any given temporal order. Counter-check: if the deploy runs first and a dispatch starts later, the deploy doesn't wait, finishes, and the dispatch proceeds. If the dispatch starts first, the deploy waits, the dispatch finishes, and the deploy proceeds. Either way acyclic. **Pros:** one-line change, preserves intent (deploys serialize behind ongoing dispatches). **Cons:** a deploy that started a second before a dispatch will jump ahead of it — but that matches real-world ordering already, since the deploy's underlying Phase 1 build preceded the dispatch trigger anyway. ### 2. Single queue via GH Actions concurrency groups Replace both custom wait loops with `concurrency: group: api-deploy, cancel-in-progress: false`. The downside: cancel-in-progress=false queues serially but doesn't give any visibility; `cancel-in-progress=true` would kill running work. And deploys-triggered-by-workflow_run and dispatches would need the *same* group key to actually serialize together. ### 3. Fold deploy.yml into dispatch.yml Have the `workflow_run`-triggered path call the same job chain via workflow_call. One queue, one wait. Bigger change but removes the duplication entirely. ## Recommendation Option 1. One-line edit to `deploy.yml` (and whichever shared template hosts it). Consider also changing the dispatch's `DEPLOYS` count to use the same ordering — today it blocks on *any* deploy even if the deploy started after it, which is correct today but worth auditing. ## Notes - Related but distinct from #7 (direct-push race with humans). This one is bot-vs-bot, no human involvement needed. - Recovery procedure when this fires: cancel all stuck runs, manually bump `api/package.json` + lockfile to the latest published versions (the stuck dispatches were trying to do this but never got past their wait step), push a commit, let the single fresh Phase 1 build → Deploy chain run alone.
nelsong6 commented 2026-04-18 21:46:47 -07:00 (Migrated from github.com)

Interim state: automated triggers disabled in api (commit api@67cffff). Running Claude-driven CI — deploys + dispatches fire via workflow_dispatch only until this is redesigned.

Revisit plan: replace both custom wait loops with native GH Actions concurrency groups. 4 lines total, two of which we already use for tofu:

```yaml

api/.github/workflows/tofu.yml already has this pattern:

concurrency:
group: tofu-${{ github.ref }}
cancel-in-progress: false
```

Same block with `group: api-deploy` in both deploy.yml and dispatch.yml gives a single queue across the two workflows — no custom wait scripts, no emergent deadlock. Likely lands with the AKS migration work in api#14 since we'll be rewriting deploys anyway.

Root-cause retrospective worth noting for posterity: the custom wait scripts were solving a problem the platform already solved. Two workflows each using "any in-progress run of the other kind" as a veto forms an emergent cycle; a concurrency group is a proper mutex and can't deadlock. Somewhere along the way we optimized for queue-position log visibility and cross-workflow coordination and walked past the native primitive that was already in the adjacent file.

Interim state: automated triggers disabled in api (commit [api@67cffff](https://github.com/nelsong6/api/commit/67cffff)). Running Claude-driven CI — deploys + dispatches fire via workflow_dispatch only until this is redesigned. Revisit plan: replace both custom wait loops with native GH Actions concurrency groups. 4 lines total, two of which we already use for tofu: \`\`\`yaml # api/.github/workflows/tofu.yml already has this pattern: concurrency: group: tofu-\${{ github.ref }} cancel-in-progress: false \`\`\` Same block with \`group: api-deploy\` in both deploy.yml and dispatch.yml gives a single queue across the two workflows — no custom wait scripts, no emergent deadlock. Likely lands with the AKS migration work in [api#14](https://github.com/nelsong6/api/issues/14) since we'll be rewriting deploys anyway. Root-cause retrospective worth noting for posterity: the custom wait scripts were solving a problem the platform already solved. Two workflows each using "any in-progress run of the other kind" as a veto forms an emergent cycle; a concurrency group is a proper mutex and can't deadlock. Somewhere along the way we optimized for queue-position log visibility and cross-workflow coordination and walked past the native primitive that was already in the adjacent file.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
romaine-life/pipeline-templates#8
No description provided.