Skip to content

Keep tree-status on PRs correct when a push run fails partway - #5239

Open
chingjun wants to merge 1 commit into
flutter:mainfrom
chingjun:cocoon-2026-12-01-resolve-reported-thread-issues
Open

chingjun wants to merge 1 commit into
flutter:mainfrom
chingjun:cocoon-2026-12-01-resolve-reported-thread-issues

Conversation

@chingjun

@chingjun chingjun commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

PushBuildStatusToGithub posts the tree status to every open PR, and records in Firestore what it posted so that later runs only post to PRs whose status has to change. The records were written in one batch at the end of the run. A run that failed before that had posted to GitHub but recorded nothing. Once the tree went back to its previous status, the stale records matched it again, and the PRs kept the wrong status ("Tree is currently broken" on a green tree) until the tree next changed or the PR got a new commit.

That has happened when a run was cut short, and when GitHub listed a PR twice, which made Firestore reject the batch write for having the same document in it twice.

PRs are now handled in batches of 20. For each batch:

  1. The records are read from Firestore.
  2. The records of the PRs that need a new status are set to "unknown".
  3. The status is posted to those PRs.
  4. The status is recorded for the PRs that GitHub accepted it for.

A run that fails partway leaves records that say "unknown", which matches no tree status, so the next run posts to those PRs again. At worst that repeats one batch of statuses that GitHub already had.

A failed Firestore write no longer fails the run. It is logged per PR, also when Firestore reports it in the response rather than as an error, which was ignored before. A PR whose record could not be set to "unknown" is skipped until the next run, and one whose new status could not be recorded is posted to again.

The list of open PRs is de-duplicated. GitHub lists a PR twice when another PR is opened while the list is fetched, a page at a time.

Also:

  • The records of a batch are read by name in one request, instead of with one query per PR. They have been named _ since they were first written (Write github build status to Firestore #3540). queryLastBuildStatus has no other callers and is removed.
  • GithubBuildStatus.documentIdFor returned the head SHA alone, which is not how the records are named. It was only used to name records in tests, and now returns the real name.
  • PRs are posted to in descending order of PR number, so that the newest get a new status first.

A change of tree status now takes up to two Firestore writes per batch of PRs that need it, instead of one per run. A run in which nothing changes makes one read per batch and no writes.

Fixes flutter/flutter#197968

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read the Flutter Style Guide recently, and have followed its advice.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

PushBuildStatusToGithub posts the tree status to every open PR, and records
in Firestore what it posted so that later runs only post to PRs whose status
has to change. The records were written in one batch at the end of the run.
A run that failed before that had posted to GitHub but recorded nothing. Once
the tree went back to its previous status, the stale records matched it
again, and the PRs kept the wrong status ("Tree is currently broken" on a
green tree) until the tree next changed or the PR got a new commit.

That has happened when a run was cut short, and when GitHub listed a PR
twice, which made Firestore reject the batch write for having the same
document in it twice.

PRs are now handled in batches of 20. For each batch:

1. The records are read from Firestore.
2. The records of the PRs that need a new status are set to "unknown".
3. The status is posted to those PRs.
4. The status is recorded for the PRs that GitHub accepted it for.

A run that fails partway leaves records that say "unknown", which matches no
tree status, so the next run posts to those PRs again. At worst that repeats
one batch of statuses that GitHub already had.

A failed Firestore write no longer fails the run. It is logged per PR, also
when Firestore reports it in the response rather than as an error, which was
ignored before. A PR whose record could not be set to "unknown" is skipped
until the next run, and one whose new status could not be recorded is posted
to again.

The list of open PRs is de-duplicated. GitHub lists a PR twice when another
PR is opened while the list is fetched, a page at a time.

Also:

- The records of a batch are read by name in one request, instead of with
  one query per PR. They have been named <head sha>_<PR number> since they
  were first written (flutter#3540). queryLastBuildStatus has no other callers and
  is removed.
- GithubBuildStatus.documentIdFor returned the head SHA alone, which is not
  how the records are named. It was only used to name records in tests, and
  now returns the real name.
- PRs are posted to in descending order of PR number, so that the newest
  get a new status first.

A change of tree status now takes up to two Firestore writes per batch of
PRs that need it, instead of one per run. A run in which nothing changes
makes one read per batch and no writes.
@chingjun
chingjun requested a review from jtmcdole December 4, 2026 23:52
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Dec 4, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the PushBuildStatusToGithub handler to process pull request status updates in batches, prioritizing newer PRs and introducing a two-phase update mechanism (marking status as unknown in Firestore before posting to GitHub) to prevent out-of-sync states during failures. It also replaces individual Firestore queries with batch operations. The review feedback suggests wrapping the execution of each batch in a try-catch block to prevent a failure in one batch from halting the processing of subsequent batches.

Comment on lines +114 to +116
for (final batch in prList.slices(batchSize)) {
await _updateBatch(github, slug, realStatus, batch, firestoreService);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To make the background sync process more resilient, we should wrap the execution of each batch in a try-catch block. This ensures that if a transient error or unhandled exception occurs while processing one batch, it does not prevent other batches of pull requests from being updated.

    for (final batch in prList.slices(batchSize)) {
      try {
        await _updateBatch(github, slug, realStatus, batch, firestoreService);
      } catch (e, s) {
        log.error('Failed to update batch of PRs: ${batch.map((pr) => pr.number).join(', ')}', e, s);
      }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[cocoon] PushBuildStatusToGithub crash on duplicate PRs leaves open PRs stuck with failing tree-status

1 participant