Skip to content

fix: use atomic write for engine.stamp to prevent race conditions#184131

Merged
auto-submit[bot] merged 7 commits into
flutter:masterfrom
b055man:fix/engine-stamp-race
Mar 26, 2026
Merged

fix: use atomic write for engine.stamp to prevent race conditions#184131
auto-submit[bot] merged 7 commits into
flutter:masterfrom
b055man:fix/engine-stamp-race

Conversation

@b055man
Copy link
Copy Markdown
Contributor

@b055man b055man commented Mar 25, 2026

Description

This PR fixes a race condition that occurs when multiple Flutter commands (e.g., parallel flutter pub get executions via tools like melos) are run concurrently.

Previously, bin/internal/update_engine_version.sh and .ps1 wrote to bin/cache/engine.stamp directly. If one process truncated the file for writing at the exact microsecond another process was reading it, the reader would receive an empty string. This caused the Flutter tool to construct a malformed URL for the engine stamp download (e.g., https://storage.googleapis.com/flutter_infra_release/flutter//engine_stamp.json), resulting in a 404 error that randomly broke CI pipelines and local builds.

Changes:

  • Modified update_engine_version.sh and update_engine_version.ps1 to use an atomic write pattern (writing the engine hash to a temporary file, then atomically moving/renaming it to engine.stamp).
  • Added a stress test in dev/tools/test/update_engine_version_test.dart that deliberately spawns 20 concurrent writers while constantly reading the stamp file to guarantee that a successful read never returns an empty string.

Issues fixed

Fixes #184027

Pre-launch Checklist

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

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@b055man b055man requested a review from jtmcdole as a code owner March 25, 2026 16:52
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Mar 25, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Copy Markdown
Contributor

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

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 updates the update_engine_version scripts (PowerShell and shell) to write the engine version to engine.stamp atomically, using a temporary file and an atomic move operation. This change aims to prevent race conditions during parallel Flutter executions. A new Dart test has been added to verify this atomic behavior under concurrent write scenarios. The review suggests improving the robustness of temporary file cleanup in both scripts: by using a try...finally block in the PowerShell script and a trap in the shell script to ensure temporary files are removed even if the scripts exit unexpectedly.

Comment thread bin/internal/update_engine_version.ps1 Outdated
Comment on lines +67 to +69
$esTmp = "$flutterRoot/bin/cache/engine.stamp.tmp.$PID"
Set-Content -Path $esTmp -Value $engineVersion -Encoding Ascii
Move-Item -Path $esTmp -Destination "$flutterRoot/bin/cache/engine.stamp" -Force
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To prevent leaving temporary files in the cache directory if an error occurs (e.g., during the Move-Item operation), it's more robust to wrap the file operations in a try...finally block. The finally block ensures the temporary file is always cleaned up.

$esTmp = "$flutterRoot/bin/cache/engine.stamp.tmp.$PID"
try {
    Set-Content -Path $esTmp -Value $engineVersion -Encoding Ascii
    Move-Item -Path $esTmp -Destination "$flutterRoot/bin/cache/engine.stamp" -Force
} finally {
    if (Test-Path -Path $esTmp) {
        Remove-Item -Path $esTmp -Force
    }
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

powershell 5.1 has try finally? that's neat.

Comment thread bin/internal/update_engine_version.sh Outdated
Comment on lines +68 to +70
pid=$$
_es_tmp="$FLUTTER_ROOT/bin/cache/engine.stamp.tmp.$pid"
echo "$ENGINE_VERSION" >"$_es_tmp" && mv "$_es_tmp" "$FLUTTER_ROOT/bin/cache/engine.stamp"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

For improved robustness, it's best to use a trap to ensure the temporary file is cleaned up if the script exits unexpectedly. This prevents leaving stale .tmp files in the cache directory.

Additionally, you can inline the process ID ($$) for conciseness.

Suggested change
pid=$$
_es_tmp="$FLUTTER_ROOT/bin/cache/engine.stamp.tmp.$pid"
echo "$ENGINE_VERSION" >"$_es_tmp" && mv "$_es_tmp" "$FLUTTER_ROOT/bin/cache/engine.stamp"
_es_tmp="$FLUTTER_ROOT/bin/cache/engine.stamp.tmp.$$"
trap 'rm -f "$_es_tmp"' EXIT
echo "$ENGINE_VERSION" >"$_es_tmp" && mv "$_es_tmp" "$FLUTTER_ROOT/bin/cache/engine.stamp"
trap - EXIT

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@jtmcdole

Additionally, you can inline the process ID ($$) for conciseness.

What's your stance on this one?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like pid=$$ so its clear to anyone else reading this, absent that I would argue for a # Generate a temp file using the $$ pid

@jtmcdole jtmcdole added the CICD Run CI/CD label Mar 25, 2026
@jtmcdole
Copy link
Copy Markdown
Member

What do you think about the trap/try-finally?

@b055man
Copy link
Copy Markdown
Contributor Author

b055man commented Mar 25, 2026

What do you think about the trap/try-finally?

@jtmcdole I think they definitely make sense. I'll update the PR.

@github-actions github-actions Bot removed the CICD Run CI/CD label Mar 25, 2026
@b055man
Copy link
Copy Markdown
Contributor Author

b055man commented Mar 25, 2026

@jtmcdole I updated the PR, adressing linter / formatting issues, adding logic for temp file removal for both ps1 and bash scripts. Additionally, the powershell script now actually has some additional logic to account for potential exceptions (it does not guarantee success on Windows though, as Move-Item -Force is not atomic, only attempts to reduce a chance of an error).
I'm actually considering skipping the newly added test when running it on Windows as I suspect it may still have a potential for being flaky (do you know how to trigger the pipeline again to run all the checks?).

@jtmcdole jtmcdole added the CICD Run CI/CD label Mar 25, 2026
jtmcdole
jtmcdole previously approved these changes Mar 25, 2026
Copy link
Copy Markdown
Member

@jtmcdole jtmcdole left a comment

Choose a reason for hiding this comment

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

lgtm-seinfeld

@jtmcdole jtmcdole requested a review from bkonyi March 25, 2026 20:24
bkonyi
bkonyi previously approved these changes Mar 25, 2026
@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 25, 2026
@b055man b055man dismissed stale reviews from bkonyi and jtmcdole via 75cba74 March 25, 2026 21:22
@github-actions github-actions Bot removed the CICD Run CI/CD label Mar 25, 2026
@auto-submit
Copy link
Copy Markdown
Contributor

auto-submit Bot commented Mar 25, 2026

autosubmit label was removed for flutter/flutter/184131, because This PR has not met approval requirements for merging. The PR author is not a member of flutter-hackers and needs 2 more review(s) in order to merge this PR.

  • Merge guidelines: A PR needs at least one approved review if the author is already part of flutter-hackers or two member reviews if the author is not a flutter-hacker before re-applying the autosubmit label. Reviewers: If you left a comment approving, please use the "approve" review action instead.

@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Mar 25, 2026
@jtmcdole jtmcdole added the CICD Run CI/CD label Mar 25, 2026
@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 26, 2026
@auto-submit auto-submit Bot added this pull request to the merge queue Mar 26, 2026
Merged via the queue into flutter:master with commit 34285bc Mar 26, 2026
159 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Mar 26, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 26, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 26, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 26, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 26, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 26, 2026
mboetger pushed a commit to mboetger/flutter that referenced this pull request Mar 26, 2026
…utter#184131)

<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->

### Description

This PR fixes a race condition that occurs when multiple Flutter
commands (e.g., parallel `flutter pub get` executions via tools like
`melos`) are run concurrently.

Previously, `bin/internal/update_engine_version.sh` and `.ps1` wrote to
`bin/cache/engine.stamp` directly. If one process truncated the file for
writing at the exact microsecond another process was reading it, the
reader would receive an empty string. This caused the Flutter tool to
construct a malformed URL for the engine stamp download (e.g.,
`https://storage.googleapis.com/flutter_infra_release/flutter//engine_stamp.json`),
resulting in a 404 error that randomly broke CI pipelines and local
builds.

**Changes:**
* Modified `update_engine_version.sh` and `update_engine_version.ps1` to
use an atomic write pattern (writing the engine hash to a temporary
file, then atomically moving/renaming it to `engine.stamp`).
* Added a stress test in
`dev/tools/test/update_engine_version_test.dart` that deliberately
spawns 20 concurrent writers while constantly reading the stamp file to
guarantee that a successful read never returns an empty string.

### Issues fixed

Fixes flutter#184027

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

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

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[AI contribution guidelines]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 26, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Mar 26, 2026
…11372)

Manual roll Flutter from 82d96ef98a33 to e79bf6cbc140 (32 revisions)

Manual roll requested by tarrinneal@google.com

flutter/flutter@82d96ef...e79bf6c

2026-03-26 engine-flutter-autoroll@skia.org Roll Dart SDK from a3af8949863e to 7587a31814c6 (1 revision) (flutter/flutter#184213)
2026-03-26 30870216+gaaclarke@users.noreply.github.com Adds rockchip series to block list for vulkan. (flutter/flutter#184207)
2026-03-26 41930132+hellohuanlin@users.noreply.github.com [ios]add ci/cd label to ios triage (flutter/flutter#184214)
2026-03-26 engine-flutter-autoroll@skia.org Roll Skia from 10c97361d8f3 to bee5a06ef578 (1 revision) (flutter/flutter#184203)
2026-03-26 engine-flutter-autoroll@skia.org Roll Fuchsia Linux SDK from rS5ezRgehkw26fKRX... to BIlBJNOlKjQeRFoFy... (flutter/flutter#184197)
2026-03-26 engine-flutter-autoroll@skia.org Roll Packages from 5909bdd to 0dd2410 (3 revisions) (flutter/flutter#184198)
2026-03-26 1776065+b055man@users.noreply.github.com fix: use atomic write for engine.stamp to prevent race conditions (flutter/flutter#184131)
2026-03-26 engine-flutter-autoroll@skia.org Roll Skia from 77dfb68002cd to 10c97361d8f3 (1 revision) (flutter/flutter#184195)
2026-03-26 engine-flutter-autoroll@skia.org Roll Dart SDK from 349dbbbdba99 to a3af8949863e (1 revision) (flutter/flutter#184194)
2026-03-26 engine-flutter-autoroll@skia.org Roll Skia from 81ef2238cb09 to 77dfb68002cd (12 revisions) (flutter/flutter#184186)
2026-03-26 jason-simmons@users.noreply.github.com Revert "Keep glyphs for variable fonts (#183857)" (flutter/flutter#184147)
2026-03-26 97480502+b-luk@users.noreply.github.com Expand simple shape path optimization logic and move it from dl_dispatcher to dl_builder (flutter/flutter#184096)
2026-03-26 jacksongardner@google.com Fix merge changelog workflow. (flutter/flutter#184145)
2026-03-26 34871572+gmackall@users.noreply.github.com Add notes on HCPP to `Android-Platform-Views.md` (flutter/flutter#183859)
2026-03-26 30870216+gaaclarke@users.noreply.github.com Adds explicit name to the cicd label job. (flutter/flutter#184070)
2026-03-26 engine-flutter-autoroll@skia.org Roll Dart SDK from 26f80b9403f5 to 349dbbbdba99 (3 revisions) (flutter/flutter#184176)
2026-03-26 34871572+gmackall@users.noreply.github.com Collect impeller analytics for appbundles (flutter/flutter#184146)
2026-03-26 737941+loic-sharma@users.noreply.github.com [Dot shorthands] Migrate examples/api/lib/material (flutter/flutter#183963)
2026-03-26 jason-simmons@users.noreply.github.com Roll Dart DevTools to a version with correct CIPD tags (flutter/flutter#184172)
2026-03-25 47866232+chunhtai@users.noreply.github.com Pipes ScrollCacheExtent through more scroll views (flutter/flutter#184078)
2026-03-25 32538273+ValentinVignal@users.noreply.github.com Add widget of the week link in SensitiveContent documentation (flutter/flutter#183972)
2026-03-25 engine-flutter-autoroll@skia.org Roll Packages from 8dcfd11 to 5909bdd (13 revisions) (flutter/flutter#184123)
2026-03-25 engine-flutter-autoroll@skia.org Roll Dart SDK from c48f0c954d86 to 26f80b9403f5 (1 revision) (flutter/flutter#184117)
2026-03-25 engine-flutter-autoroll@skia.org Roll Skia from 51725ae49ef5 to 81ef2238cb09 (3 revisions) (flutter/flutter#184115)
2026-03-25 engine-flutter-autoroll@skia.org Roll Fuchsia Linux SDK from M3sjWggTQmP2AD4bS... to rS5ezRgehkw26fKRX... (flutter/flutter#184114)
2026-03-25 104349824+huycozy@users.noreply.github.com Add SensitiveContent widget sample code (flutter/flutter#183846)
2026-03-25 rmolivares@renzo-olivares.dev SelectableRegion should passthrough constraints to child unmodified (flutter/flutter#184083)
2026-03-25 engine-flutter-autoroll@skia.org Roll Dart SDK from ce171d5026ff to c48f0c954d86 (2 revisions) (flutter/flutter#184105)
2026-03-25 engine-flutter-autoroll@skia.org Roll Skia from 789ad6b12776 to 51725ae49ef5 (3 revisions) (flutter/flutter#184106)
2026-03-25 engine-flutter-autoroll@skia.org Roll Skia from f4e59f82dc69 to 789ad6b12776 (7 revisions) (flutter/flutter#184098)
2026-03-25 engine-flutter-autoroll@skia.org Roll Dart SDK from b08bd0a3ee39 to ce171d5026ff (4 revisions) (flutter/flutter#184095)
2026-03-24 srawlins@google.com [flutter_goldens] Remove dead check on null being in a list of non-nullables (flutter/flutter#183938)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC stuartmorgan@google.com,tarrinneal@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

...
jason-simmons added a commit to jason-simmons/flutter that referenced this pull request Mar 31, 2026
ahmedsameha1 pushed a commit to ahmedsameha1/flutter that referenced this pull request Apr 14, 2026
…utter#184131)

<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->

### Description

This PR fixes a race condition that occurs when multiple Flutter
commands (e.g., parallel `flutter pub get` executions via tools like
`melos`) are run concurrently.

Previously, `bin/internal/update_engine_version.sh` and `.ps1` wrote to
`bin/cache/engine.stamp` directly. If one process truncated the file for
writing at the exact microsecond another process was reading it, the
reader would receive an empty string. This caused the Flutter tool to
construct a malformed URL for the engine stamp download (e.g.,
`https://storage.googleapis.com/flutter_infra_release/flutter//engine_stamp.json`),
resulting in a 404 error that randomly broke CI pipelines and local
builds.

**Changes:**
* Modified `update_engine_version.sh` and `update_engine_version.ps1` to
use an atomic write pattern (writing the engine hash to a temporary
file, then atomically moving/renaming it to `engine.stamp`).
* Added a stress test in
`dev/tools/test/update_engine_version_test.dart` that deliberately
spawns 20 concurrent writers while constantly reading the stamp file to
guarantee that a successful read never returns an empty string.

### Issues fixed

Fixes flutter#184027

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

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

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[AI contribution guidelines]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
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.

Race condition in update_engine_version scripts causes 404 for engine_stamp.json on parallel flutter command invocations

3 participants