Skip to content

Make bump_mtime monotonic so same-tick rewrites never share a stat key - #2968

Merged
J. Nick Koston (bdraco) merged 1 commit into
mainfrom
fix-bump-mtime-same-tick
Oct 24, 2026
Merged

J. Nick Koston (bdraco) merged 1 commit into
mainfrom
fix-bump-mtime-same-tick

Conversation

@bdraco

Copy link
Copy Markdown
Member

What does this implement/fix?

Makes the test helper bump_mtime monotonic per file so two rewrites of the same file inside one filesystem clock tick never end up with the same (mtime, size) stat key. Fixes the flaky test_digest_moves_only_on_content_in_play failure on windows-latest.

# before
stat = path.stat()
os.utime(path, (stat.st_atime, stat.st_mtime + 10))

# after
stat = path.stat()
mtime = max(stat.st_mtime, _BUMPED_MTIMES.get(path, 0.0)) + 10
_BUMPED_MTIMES[path] = mtime
os.utime(path, (stat.st_atime, mtime))
  • The test rewrites secrets.yaml twice with the same byte length (abc -> abz) and bumps after each; when both writes land in the same Windows clock tick, both bumps compute the same mtime, the secrets cache sees an unchanged stat key and serves the stale parse, and rotated != baseline fails.
  • Scope is the shared helper only; all twelve call sites rewrite then bump, so a strictly increasing mtime is what each of them wants.
  • Seen on deps: bump pre-commit from 4.6.2 to 4.7.0 #2962 (Pytest windows-latest / Python 3.14); the other runners' finer mtime granularity hides it.
Verification

Replayed the failing sequence with every rewrite pinned to one mtime (what a same-tick write looks like): the old helper reports the fingerprint unchanged after the abz rotation, the new one reports it moved. tests/test_device_yaml.py, tests/test_device_scanner_branches.py and tests/test_mqtt_coordinator_extract.py (the files using the helper) pass locally, 439 tests.

Related issue or feature (if applicable):

Types of changes

  • Bugfix (non-breaking change which fixes an issue) — bugfix
  • New feature (non-breaking change which adds functionality) — new-feature
  • Enhancement to an existing feature — enhancement
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) — breaking-change
  • Refactor (no behaviour change) — refactor
  • Documentation only — docs
  • Maintenance / chore — maintenance
  • CI / workflow change — ci
  • Dependencies bump — dependencies

Frontend coordination

  • No frontend change needed
  • Companion frontend PR: esphome/device-builder-frontend#

Checklist

  • The code change is tested and works locally.
  • Pre-commit hooks pass (ruff, codespell, yaml/json/python checks).
  • Tests have been added or updated under tests/ where applicable.
  • components.index.json / definitions/components/*.json have not been hand-edited (regenerate via script/sync_components.py if a sync is needed).
  • Architecture-level changes are reflected in docs/ARCHITECTURE.md and/or docs/API.md.

@bdraco

Copy link
Copy Markdown
Member Author

ESPHome Bot (@esphbot) review

@bdraco
J. Nick Koston (bdraco) marked this pull request as ready for review October 24, 2026 03:26
Copilot AI balanced review requested due to automatic review settings October 24, 2026 03:26
@codspeed

codspeed Bot commented Oct 24, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 28 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing fix-bump-mtime-same-tick (d28dc07) with main (9b80c10)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports. ↩

Copilot AI left a comment

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.

🟢 Approval recommended

The localized test-helper change prevents repeated timestamp collisions while preserving existing caller behavior.

No open issues 🎉

What changed in this PR

Updates the shared bump_mtime test helper to prevent timestamp collisions during same-tick rewrites, addressing the Windows test flake observed in #2962 without changing production code.

Changes:

  • Tracks the last bumped modification time per path.
  • Advances each timestamp beyond both the current file timestamp and its previous bump.
  • Updates the helper’s docstring to describe the monotonic behavior.
File Description
tests/​conftest.py Makes repeated timestamp bumps strictly increasing per path.

Review effort: Balanced

@esphbot

Copy link
Copy Markdown
Contributor

PR Review — Make bump_mtime monotonic so same-tick rewrites never share a stat key

Correct, minimal fix for the Windows same-tick flake. Ready to merge.

What works:

  • I confirmed the root cause in test_digest_moves_only_on_content_in_play (tests/test_device_yaml.py:4466-4473). Two rewrites of secrets.yaml with the same size can land in the same clock tick. Each write_text resets the mtime to the same value, so the old st_mtime + 10 gave both bumps the same stat key and the cache served the stale parse.
  • max(stat.st_mtime, previous_bump) + 10 makes later bumps strictly larger. It still respects a real mtime that has moved past the last bump, so callers that rely on 'later than the current mtime' behave as before.
  • The scope matches the description. All 11 call sites (test_device_yaml.py, test_device_scanner_branches.py, test_mqtt_coordinator_extract.py) rewrite and then bump, so a strictly increasing mtime is what each one wants. The PR description says twelve call sites; I count 11.
  • The docstring is a one-line contract, as AGENTS.md asks.

Notes (no action needed):

  • _BUMPED_MTIMES is module-global and lives for the whole session. Callers pass paths under the per-test tmp_path, so no two tests share a key. Each xdist worker is its own process. The dict grows by about one entry per call site, which doesn't matter.


Checklist

  • Fix addresses the stated root cause
  • No behaviour change for existing callers
  • Test isolation (shared state across tests)
  • Docstring follows repo one-line contract convention

Automated review by Kōan (Claude) HEAD=d28dc07 26s

@esphbot ESPHome Bot (esphbot) left a comment

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.

Tip

No blocking issues found — ready to merge.

@codecov

codecov Bot commented Oct 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.76%. Comparing base (9b80c10) to head (d28dc07).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2968   +/-   ##
=======================================
  Coverage   99.76%   99.76%           
=======================================
  Files         300      300           
  Lines       26429    26429           
=======================================
  Hits        26367    26367           
  Misses         62       62           
Flag Coverage Δ
py3.12 99.73% <ø> (ø)
py3.14 99.61% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bdraco
J. Nick Koston (bdraco) merged commit db12865 into main Oct 24, 2026
28 checks passed
@bdraco
J. Nick Koston (bdraco) deleted the fix-bump-mtime-same-tick branch October 24, 2026 03:35
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bugfix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants