Skip to content

Do not run two PR workflows at the same commit - #116701

Merged
maxknv merged 3 commits into
ClickHouse:masterfrom
groeneai:ci-pr-workflow-concurrency-group
Sep 2, 2026
Merged

maxknv merged 3 commits into
ClickHouse:masterfrom
groeneai:ci-pr-workflow-concurrency-group

Conversation

@groeneai

@groeneai groeneai commented Aug 27, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #116641
Related: #116642

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Give the pull_request workflows a concurrency group so two runs cannot race at one commit.

Description

A PR is opened and reopened seconds later, so two pull_request events dispatch two full PR runs
at one head commit. Both are run_attempt = 1 and neither supersedes the other, because the
generated workflow has no concurrency block. praktika keys their shared state by (PR, sha),
never by RUN_ID, so each run overwrites the other's S3 artifacts and reads its in-flight statuses
from result_<workflow>.json.

On #116641 nine legs of one run died in the pre-run download with PreconditionFailed:
the other run re-uploaded the key mid-download, invalidating the ranged GET's ETag. All nine are
success in the sibling run at the identical tree, two of them flaky check. On #116642 the
losing Finish Workflow read that shared document mid-run and published
No per-arch Bugfix Validation job validated the bug, which the sibling run contradicts with three
success jobs.

This adds a concurrency group of workflow, PR number and head sha with cancel-in-progress, for
pull_request only. The head sha is in the group, so a push starts a new group and never cancels
the previous run. master.yml and release_branches.yml share the template and stay
byte-identical, because github.event.pull_request is null on a push. The generator now refuses to
emit a pull_request workflow without that block, or another workflow referencing it.

The close-and-reopen comes from an app outside this repository that this change does not touch.

Residuals: CacheRunnerHooks.pre_run can still aim a download at another PR's prefix,
any_bugfix_validation_passed still reads a RUNNING per-arch job as a refutation, and
Finish Workflow is emitted with always(), so the group cannot stop a superseded finalizer that
already started; in the measured 8-second window only Config Workflow exists.

The two runs and their shared report:
33040674324,
33040682270,
report.


Workflow [PR]
Sync PR [sync-upstream/pr/116701]

Version info

  • Merged into: 26.9.1.513 (included in 26.9 and later)

A PR that is opened and then reopened seconds later delivers two pull_request
events at one head commit, so GitHub dispatches two complete PR workflow runs in
parallel. Both are run_attempt = 1, and TEMPLATE_PULL_REQUEST_0 emits no
concurrency block, unlike TEMPLATE_SCHEDULE and TEMPLATE_DISPATCH_WORKFLOW, so
neither run supersedes the other.

Everything the two runs share is keyed by (PR, sha) and never by RUN_ID: the S3
artifact objects from _Environment.get_s3_prefix_static, and the
result_<workflow>.json document merged by _ResultS3.update_workflow_results. Two
consequences measured at one commit of ClickHouse#116641 and of ClickHouse#116642:

  - Nine legs of one run died in the praktika pre-run artifact download with
    PreconditionFailed, because the other run re-uploaded the key while the
    ranged GET was in flight, so its ETag precondition no longer held. All nine
    are success in the sibling run at the identical tree, and two of them are
    flaky check legs. praktika had already closed this same race for the
    submodule cache object alone, with an If-None-Match write-once create in
    native_jobs.py.

  - The losing run's Finish Workflow read the shared result document while the
    other run's per-arch jobs were still RUNNING, and published "No per-arch
    Bugfix Validation job validated the bug". The sibling run contradicts it
    with three success jobs.

Emit a concurrency group of workflow, PR number and head sha, with
cancel-in-progress, for pull_request workflows. The head sha is in the group so
a push starts a new group and never cancels the run at the previous commit. When
the second run appears the first has created exactly one job, Config Workflow,
so a supersession cancels that job and nothing else.

The group is populated per event rather than written into the template, because
TEMPLATE_PULL_REQUEST_0 is shared with the push workflows, where
github.event.pull_request is null. A literal in the template would give MasterCI
a degenerate group and, with cancel-in-progress, cancel every in-flight run on
every master commit. master.yml and release_branches.yml stay byte-identical.

The close-and-reopen originates from an app outside this repository. This change
neither depends on it nor alters it, and a genuinely reopened PR still gets a
run.

retry_infra_failures.yml partially masks this today: a pre-run S3 failure is
classified as infrastructure by its rule for a Run step that failed in under two
minutes, and the run is retried.

Co-Authored-By: Claude Opus 5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-own-pr PR origin: CI on a groeneai PR exposed a separate defect labels Aug 27, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold
review of the resulting tree, done before reading the implementation notes.

The independent pass read the generator in full, its callers, every event sibling and the
generated workflow control paths, and reported no finding. Its diff scope matched the intended
change set exactly (3 files, +23/-1, no untracked files), so the empty result is not a
wrong-base artifact.

My own review found four nits and no blocker or major. Three were defects in the PR body and
are fixed above: it had no visible Related: line, its description sat exactly on my length
budget, and it did not state the change's one visible cost, which is that a superseded run
leaves a cancelled conclusion on its single Config Workflow check-run.

# Sev Finding Verdict Evidence / action
1 💡 Literal names left outside inline code blocks in the commit message and in the two added comments, where AGENTS.md asks for them AGREE Real and it predates this branch base. Not fixed: cosmetic, and correcting the commit message would mean amending a branch, which the same rulebook forbids. The description above complies.
2 💡 No visible Related: line: the linkage lived only in the template's HTML comment, which does not render AGREE, fixed Two Related: lines with full URLs for the two pull requests the defect was measured on
3 💡 Description at the length ceiling, no room for the lines finding 2 requires AGREE, fixed Condensed; template headers re-verified byte-identical to .github/PULL_REQUEST_TEMPLATE.md
4 💡 The one visible cost of cancel-in-progress was unstated AGREE, fixed Named in the description

What I checked myself and could not turn into a finding: the brace count in the new template
(4, because it is substituted in the generator's first format pass and reduced only by the
second, matching TEMPLATE_ENV_CHECKOUT_REF_PR rather than the 8 that in-template literals
need); that both arms of the event branch set the placeholder, so no KeyError path exists;
the carrier list re-derived from the five-member Workflow.Event enum rather than from the
plan, giving exactly the two pull_request workflows changed and the two push workflows
untouched; that retry_infra_failures.yml selects runs whose conclusion is failure, so a
run cancelled by the new group is never retried; and that a synchronize push lands in a
different group and therefore cancels nothing.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-11:20260827-115300

@clickhouse-gh clickhouse-gh Bot added manual approve Manual approve required to run CI labels Aug 27, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Partial, and by nature. The trigger is a pull_request event delivered by an app outside this repository, so there is no local repro. The property under change is deterministic: on a pristine tree grep -c '^concurrency:' .github/workflows/pull_request.yml is 0; with the change it is 1 and yaml.safe_load parses the group. Field occurrence is measured: 26 duplicated run_attempt = 1 run groups over 38 h of workflow 16441423, all at a PR's first commit.
b Root cause explained? TEMPLATE_PULL_REQUEST_0 emits no concurrency: block, unlike TEMPLATE_SCHEDULE and TEMPLATE_DISPATCH_WORKFLOW, so two pull_request events at one head commit both run to completion. Every piece of state they share is keyed by (PR, sha) and never by RUN_ID: the S3 artifact objects and result_<workflow>.json. One run re-uploads an artifact key while the other is mid-download, invalidating the ranged GET's ETag (PreconditionFailed) and dropping that leg's dependee closure; one run's Finish Workflow reads the other's in-flight statuses and verdicts on them.
c Fix matches root cause? Yes. The cause is that two runs at one commit are permitted while sharing (PR, sha) state; the change removes the permission at the layer that grants it. Retrying PreconditionFailed or guarding the bugfix hook would each fix one of four consequences. praktika already closed this exact ETag race for a single object (native_jobs.py:281-291, whose comment names it); this addresses the cause rather than the next object.
d Test intent preserved / new tests added? No test weakened or removed. pytest ci/tests/ -q: 1141 passed, 12 failed, and those 12 are proven pre-existing by an A/B against a pristine tree (identical 12 names, empty git diff HEAD); neither failing file references the changed code. No file added under ci/tests/, deliberately: that directory is being removed (#115650 is open).
e Both directions demonstrated? Yes. Without the change the block is absent (grep -c = 0, yaml.safe_load gives None); with it the block is present and parses. Plus a two-way negative control: arming the PUSH branch with the same template makes master.yml and release_branches.yml gain the block (4 files changed); disarming restores both to their baseline md5 (2 files changed).
f Fix is general across code paths? Fixed in the generator, not hand-patched into generated YAML. Carriers enumerated over all 26 workflow definitions and closed against the five-member Workflow.Event enum: both PULL_REQUEST workflows (PR, BackportPR) get the block; both PUSH workflows (MasterCI, ReleaseBranchCI) share the template and are asserted byte-identical; MERGE_QUEUE, SCHEDULE and DISPATCH use other templates. Of 33 tracked workflow files, exactly 2 change and 31 are byte-identical by blob hash.
g Fix generalizes across inputs (params/datatypes/wrappers)? The inputs here are event payloads. Every pull_request action (opened, reopened, synchronize, and the rest) populates github.event.pull_request.head.sha, so the group is always well defined. A push leaves that object null, which is why nothing is emitted there, and the null case is proven never to render. Boundary case: the head sha changing (a push) forms a new group and cancels nothing, which is the deliberate design point.
h Backward compatible? (maintainer-approved exception only) No setting, no serialization format, no SettingsChangesHistory.cpp entry. Reverting is deleting three YAML lines and two generator hunks. One behaviour change, stated in the description: a second dispatch at one commit now supersedes the first, with a supersession window measured at one job (Config Workflow).
i Invariants and contracts preserved? The invariant is that a group naming github.event.pull_request.* may only be emitted for a trigger that carries a pull request; it is enforced per event and demonstrated by the control in (e). Top-level concurrency is valid schema (yaml.safe_load parses it, and team_review_requests.yml:8-10 is an existing hand-written precedent for the same spelling with cancel-in-progress: true). Exactly one concurrency key per file and no per-job concurrency, so nothing is shadowed. The blank line before env: is what keeps the empty render byte-identical. praktika's own _check_yaml_up_to_date requires the regenerated YAML to be committed, and both files are in this commit.

Session id: cron:clickhouse-impl-slot-41:20260827-112800

@clickhouse-gh

clickhouse-gh Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4a58024]

Summary: ✅


AI Review

Summary

This PR adds a top-level concurrency guard to the generated pull_request workflows and hardens YamlGenerator so only pull_request workflows may reference github.event.pull_request, while the push workflows that share the template stay unchanged. After reviewing the current diff, prior discussion, generated workflow output, and a local PYTHONPATH=./ci:. python3 -m praktika yaml regeneration on the current tree, I did not find any remaining correctness, safety, or coverage issues.

Final Verdict

✅ No new findings.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @leshikus @maxknv, could you review this? A PR reopened seconds after opening delivers two pull_request events at one head commit, so two full PR runs race over state praktika keys by (PR, sha) alone: nine legs of one run died in the pre-run artifact download with PreconditionFailed, and the losing Finish Workflow published a Bugfix Validation verdict read off the other run's still-running jobs. It adds a concurrency group of workflow, PR number and head sha for pull_request events only, so the two push workflows that share the template stay byte-identical.

Comment thread ci/praktika/yaml_generator.py Resolved
`_check_yaml_up_to_date` only compares the committed YAML against a fresh
regeneration, so removing `{CONCURRENCY}` from `TEMPLATE_PULL_REQUEST_0` and
committing the regenerated files keeps CI green while both `pull_request`
workflows silently lose the group: measured, `praktika yaml` exits 0 and
`pull_request.yml` and `backport_branches.yml` come back without it.

The generator now refuses to emit a `pull_request` workflow that has no group,
and refuses to emit any other workflow that references
`github.event.pull_request`. `Config Workflow` regenerates on every run of
every workflow, so the check is enforced in CI and locally alike.
Checking only for `concurrency:` and `github.event.pull_request.number` still
passed after `cancel-in-progress: true` was dropped, and after the head sha was
dropped from the group, because the sha also reaches the rendered file through
`CHECKOUT_REF`. Comparing against `TEMPLATE_CONCURRENCY_PR` itself is no
better: mutating the template mutates both sides of that comparison, so
deleting `cancel-in-progress: true` from it leaves `praktika yaml` at exit 0
and a rendered `pull_request.yml` with no such line.

Spelling the expected block out at the assert makes each of the four mutations
that break the contract fail generation: `{CONCURRENCY}` removed from
`TEMPLATE_PULL_REQUEST_0`, `cancel-in-progress` removed, the head sha removed,
the pull request number removed.
@maxknv
maxknv added this pull request to the merge queue Sep 2, 2026
Merged via the queue into ClickHouse:master with commit 23c3034 Sep 2, 2026
179 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 2, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 3, 2026
The only job blocking `Mergeable Check` on the previous head was
`Stress test (arm_asan_ubsan)`, a `DB::collectSets` stack exhaustion that fired on
eight unrelated pull requests and is fixed on master by ClickHouse#116978. ClickHouse#116701, also now
on master, stops two PR workflows running at the same commit, which produced the
other two reds on that head. Neither commit was reachable from this branch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors groeneai-origin-ci-own-pr PR origin: CI on a groeneai PR exposed a separate defect manual approve Manual approve required to run CI pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants