Sub topic 14
Troubleshooting unexpected cancellations
This chapter assumes you know basic workflow syntax and triggers, and that you can read ${{ }} expressions and the github context. It also assumes you have met concurrency, group and cancel-in-progress in the earlier chapters. Here you will learn to find out why a run was cancelled and to fix the cause.
Start from the evidence
A cancelled run is not always a bug. Concurrency cancels runs on purpose. The question is whether the cancellation matched your intent. Collect three facts before you change any YAML:
- Which run was cancelled, and which run replaced it?
- What was the resolved group name of each one?
- Was the cancelled run in progress or pending?
Read the annotation
Open the cancelled run and look at the annotations at the top of the summary. When concurrency cancels a run, GitHub writes a message that names the group:
Canceling since a higher priority waiting request for 'CI-refs/heads/main' existsThe quoted text is the resolved group name. Compare it with the group name of the run you believe replaced it. If the names are identical, the two runs competed, and the problem is how the name was built.
If a run has no such message and no one pressed Cancel, check for these other causes:
- A person or a script cancelled it.
- A
timeout-minuteslimit ended it. That shows as a failure, not a cancellation. - A job failed under
fail-fastin a matrix, which cancels the sibling jobs.
Print the group name
The most reliable debugging step is to print what your expression produces. Add a step that echoes the values used in the group:
name: CI
on:
push:
branches: [main]
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Show concurrency inputs
run: |
echo "workflow: ${{ github.workflow }}"
echo "event: ${{ github.event_name }}"
echo "ref: ${{ github.ref }}"
echo "head_ref: ${{ github.head_ref }}"
echo "run_id: ${{ github.run_id }}"
- uses: actions/checkout@v4
- run: echo "Run tests here"The step cannot show the final group string, but the pieces let you work it out. Run it for a push and for a pull request. An empty head_ref on a push is expected, because that value only exists on pull request events.
List cancelled runs from the CLI
To see a pattern across many runs, query them with the GitHub CLI:
gh run list --workflow ci.yml --status cancelled --limit 20 \
--json databaseId,event,headBranch,createdAt,displayTitleIf many cancelled runs share a branch and fall seconds apart, concurrency is working as designed. If they come from different branches or events, the group name is probably too coarse.
A decision flow for diagnosis
The diagram follows the order in which the usual causes should be checked.
Common causes and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| Runs on different branches cancel each other | Group is a constant such as `ci` | Add `github.ref` or `github.head_ref` to the name |
| Two different workflows cancel each other | Same `name:` in both files, so `github.workflow` matches | Give each workflow a unique name, or add a fixed prefix to the group |
| Push runs on `main` cancel each other | `cancel-in-progress: true` applies to every ref | Use `${{ github.ref != 'refs/heads/main' }}` |
| A queued run disappears, though `cancel-in-progress` is `false` | A newer run replaced the older pending run | Expected behavior. Only one run can be pending per group |
| Matrix jobs cancel their siblings | Job-level group does not include a matrix value | Add `${{ matrix.os }}` or similar to the group |
| Called workflow is cancelled immediately | Caller and called workflow use the same group | Use a different group in the reusable workflow |
| Fork pull requests cancel each other | Two forks use the same branch name, so `head_ref` collides | Use the pull request number in the group |
| `prod` and `Prod` serialize against each other | Group names are case-insensitive | Use one consistent casing |
Group names that are too coarse
The most common mistake is a group that is wider than the set of runs that should compete. Consider this workflow-level setting:
concurrency:
group: ci
cancel-in-progress: trueEvery run of every branch and pull request joins the group ci. A push to a feature branch cancels the run on main. Include the pieces that define "related":
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}For pull request events, github.ref looks like refs/pull/42/merge. It is unique per pull request, so you do not need head_ref here.
The pending run that vanishes
A group allows one running and one pending run. When a third run arrives, the pending one is cancelled and the newest takes its place. That happens even with cancel-in-progress: false. In a deployment group this is usually acceptable, because the newest commit is the one you want. It is wrong if each run must happen, for example when each commit publishes a numbered release. In that case, use a unique group per run, or move the ordering into your release process.
Matrix jobs
Job-level concurrency is evaluated per job. In a matrix, every job expands from the same definition, so a fixed group puts them all in competition:
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
concurrency:
group: test-${{ github.ref }}-${{ matrix.os }}
cancel-in-progress: true
steps:
- uses: actions/checkout@v4
- run: echo "Testing on ${{ matrix.os }}"Without ${{ matrix.os }}, the Windows job and the Ubuntu job share one group, and one is cancelled or left pending.
Reusable workflows
A caller and the workflow it calls both accept a concurrency key. If both resolve to the same group, the called workflow waits on the group that the caller already holds. GitHub detects the deadlock and cancels it with a message like this:
Canceling since a deadlock for concurrency group 'deploy-main' was detected between 'top level workflow' and 'deploy'The fix is to set concurrency in only one place, or to use clearly different group names in the two files.
Expressions that resolve to nothing
An expression that evaluates to an empty string can silently widen a group. If you write group: deploy-${{ github.head_ref }} and the workflow also runs on push, the group becomes deploy- for every push. All pushes then compete. Use a fallback such as github.head_ref || github.run_id, or use github.ref, which is always set.
Also remember which contexts are available. At the workflow level you can use github, inputs and vars. The env and secrets contexts are not available, and an expression that uses them fails validation instead of producing a quiet empty value.
Cleanup in cancelled runs
If a cancellation is correct but leaves a shared resource locked, fix the cleanup. A step marked if: ${{ cancelled() }} runs when the run is cancelled. A step marked if: ${{ always() }} runs on success, failure and cancellation. Use cancelled() when the cleanup should happen only on cancellation, and always() when it should happen every time. Keep cleanup short and idempotent, because a cancelled job has limited time to finish.
jobs:
integration:
runs-on: ubuntu-latest
concurrency:
group: integration-env-${{ github.ref }}
cancel-in-progress: true
steps:
- uses: actions/checkout@v4
- run: echo "Run integration tests against a shared environment"
- name: Release environment
if: ${{ cancelled() }}
run: echo "Clean up after a cancelled run"A repeatable checklist
- Open the cancelled run and read the annotation for the group name.
- Find the run that replaced it, and compare its group name.
- If the names match but the runs should not compete, widen the name with
github.ref, a pull request number or a matrix value. - If the runs should compete but the wrong one was cancelled, review
cancel-in-progress. Check for the pending-run replacement rule. - Check whether a reusable workflow or a second workflow with the same
nameshares the group. - Check that cleanup steps use
cancelled()oralways().
Exercise
A repository has two workflow files, both with name: CI, one for unit tests and one for lint. Each contains concurrency: { group: "${{ github.workflow }}-${{ github.ref }}", cancel-in-progress: true }. Developers report that a push often shows one of the two workflows as cancelled, although nothing else was pushed. What is the cause, and what is the fix?
Show answer
Both workflows resolve github.workflow to "CI", so for the same ref they produce the same group name and cancel each other. Rename one workflow, for example "CI Unit Tests" and "CI Lint", or add a distinct fixed prefix to each group so the names differ.
Exercise
A deployment workflow uses group: deploy-production with cancel-in-progress: false. Three pushes to main arrive within one minute. The first deployment is running. What happens to the second and third runs?
Show answer
The first run keeps running. The second run becomes pending, and when the third run arrives, it replaces the second, which is cancelled. When the first finishes, the third run starts. A pending run is replaced even though cancel-in-progress is false.