Skip to content

fix(ci): snapshot+restore + broken-marker for recompact crash (#2873) - #2874

Merged
zackees merged 2 commits into
masterfrom
fix/2873-recompact-snapshot-restore
Jun 6, 2026
Merged

zackees merged 2 commits into
masterfrom
fix/2873-recompact-snapshot-restore

Conversation

@zackees

@zackees zackees commented Jun 6, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes #2873. Follows up #2860 (closed #2857) which defused the recompact crash cascade by renaming .ninja_deps out of the way before invoking the maintenance step. That worked but discarded valid deps records once per 24h, silently degrading one build's worth of header-driven incremental rebuilds.

This PR replaces the rename with a copy-based snapshot + restore + broken-marker scheme so the deps log stays intact.

Design

Three markers in build_dir:

Marker Purpose
.ninja_deps_maintenance 24h cooldown timestamp (kept from #2860)
.ninja_deps_recompact_broken new: once set, recompact is skipped forever on this build dir
.ninja_deps.bak new: snapshot of the original log; restored on failure, deleted on success

Algorithm in perform_ninja_maintenance():

  1. If .ninja_deps_recompact_broken exists → return immediately.
  2. Honor 24h cooldown via .ninja_deps_maintenance.
  3. shutil.copy2(.ninja_deps, .ninja_deps.bak) so we can restore on failure.
  4. Run ninja -t recompact.
  5. Touch the cooldown marker regardless of outcome.
  6. On success: delete .bak, log success.
  7. On failure: restore .bak over .ninja_deps, touch broken marker, log warning.

Behavior comparison

Scenario PR #2860 This PR
Healthy host deps lost once / 24h no behavior change
Windows + corrupt log, 1st run 0s, deps lost 12s crash, deps survive (restored)
Windows + corrupt log, 2nd+ run 0s, deps lost again at next 24h 0s, skipped via broken marker, deps untouched
Incremental-rebuild correctness loss once per 24h never (after first run)

Trade-off: one visible cascade per build dir lifetime in exchange for zero data loss and zero recurrence.

Test plan

ci/tests/test_ninja_maintenance.py updated to pin the new contract — 8/8 pass via uv run pytest ci/tests/test_ninja_maintenance.py -v:

  • test_snapshot_taken_then_discarded_on_success — .ninja_deps survives, .bak cleaned up
  • test_snapshot_restored_on_recompact_failure — .ninja_deps bytes identical to pre-call
  • test_broken_marker_set_on_recompact_failure — broken marker created on failure
  • test_broken_marker_skips_future_runs — RunningProcess never invoked when broken marker present
  • test_stale_bak_is_replaced — updated for new semantics (pre-existing .bak is overwritten before recompact runs)
  • test_marker_touched_on_recompact_failure — defense in depth: cooldown marker still touched on failure
  • test_within_cooldown_short_circuits — existing 24h skip still works
  • test_no_deps_file_is_a_clean_no_op — empty build dir runs cleanly

bash lint is green.

Acceptance (from #2873)

  • bash test --cpp on Windows hits the cascade at most once per build dir, then never again (broken marker short-circuits future runs).
  • .ninja_deps is byte-identical before and after a failed-recompact run (pinned by test_snapshot_restored_on_recompact_failure).
  • Subsequent bash test --cpp runs on the same build dir do not invoke ninja -t recompact (pinned by test_broken_marker_skips_future_runs).
  • Healthy hosts see no behavior change vs the pre-fix(ci): defuse recompact crash cascade on Windows (#2857) #2860 code path beyond the cooldown marker being touched on failure.

Out of scope

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved build-system reliability on Windows: recompact failures now restore the original build-dependency data, set a persistent failure marker to skip repeated failing attempts, and emit clearer warnings.
  • Chores

    • Switched maintenance to a copy-based snapshot-and-restore workflow with a cooldown marker to reduce unsafe retries.
  • Tests

    • Added/updated tests covering snapshot success, restore-on-failure, persistent failure marker behavior, skipping future runs, and snapshot-failure handling.

PR #2860 stopped the recompact crash cascade by renaming .ninja_deps
out of the way before invoking the maintenance step. That defused the
noise but discarded valid deps records once per 24h, silently degrading
one build's worth of header-driven incremental rebuilds (a header
change wouldn't trigger a recompile of any TU that wasn't otherwise
dirty, until that TU was rebuilt for some other reason).

Replace the rename with a copy-based snapshot + restore + broken-marker
scheme so the deps log stays intact:

  * Skip recompact entirely if .ninja_deps_recompact_broken exists.
    Recompact is an optimization, not a correctness requirement.
  * Snapshot .ninja_deps -> .ninja_deps.bak before invoking recompact.
  * On success: delete the snapshot, log success.
  * On failure: restore the snapshot over .ninja_deps, touch the
    broken marker so future calls short-circuit, log a warning.
  * Touch the 24h cooldown marker on every attempt (kept from #2860).

Result vs PR #2860:

  Healthy host:        no behavior change.
  Broken host (1st):   12s crash cascade once, deps survive (restored).
  Broken host (2nd+):  0s, skipped via broken marker, deps untouched.
  Incremental loss:    never (after first run) — was once per 24h.

Trade-off: one visible cascade per build dir lifetime in exchange for
zero data loss and zero recurrence. Upstream root cause tracked at
ninja-build/ninja#2787.

Test suite (ci/tests/test_ninja_maintenance.py) updated to pin the new
contract:

  * test_snapshot_taken_then_discarded_on_success
  * test_snapshot_restored_on_recompact_failure
  * test_broken_marker_set_on_recompact_failure
  * test_broken_marker_skips_future_runs
  * test_stale_bak_is_replaced (updated for new semantics)
  * existing cooldown / marker / no-deps no-op tests retained

Closes #2873

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 6, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 578da31b-dd28-4867-b08a-144799410557

📥 Commits

Reviewing files that changed from the base of the PR and between 9c6429c and 5cd3478.

📒 Files selected for processing (2)
  • ci/meson/build_config.py
  • ci/tests/test_ninja_maintenance.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • ci/meson/build_config.py
  • ci/tests/test_ninja_maintenance.py

📝 Walkthrough

Walkthrough

This PR changes perform_ninja_maintenance() to snapshot .ninja_deps to .ninja_deps.bak before ninja -t recompact, restores the snapshot and sets .ninja_deps_recompact_broken on failure to skip future attempts, and updates tests to cover success, failure, and snapshot-failure behaviors.

Changes

Ninja Maintenance Workaround

Layer / File(s) Summary
Sentinel check and snapshot setup
ci/meson/build_config.py
Imports shutil, documents the snapshot/restore + broken-sentinel contract, returns early when .ninja_deps_recompact_broken exists, and copies .ninja_deps → .ninja_deps.bak before running recompact (handles snapshot failure by touching cooldown and broken markers and returning).
Post-run handling and broken marker
ci/meson/build_config.py
Touches the 24h cooldown marker after every attempt; on success deletes .ninja_deps.bak; on failure restores .ninja_deps from .ninja_deps.bak, touches .ninja_deps_recompact_broken, and emits a stderr warning.
Test coverage for snapshot, restore, and broken sentinel
ci/tests/test_ninja_maintenance.py
Rewrites module docstring and tests: asserts snapshot is discarded on success, original bytes are restored on failure, broken marker is created and causes future short-circuiting, updates cooldown assertions, and adds a test for snapshot-copy failure that skips running ninja and flags broken.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related issues

Possibly related PRs

  • FastLED/FastLED#2860: Both PRs modify perform_ninja_maintenance() and test coverage for .ninja_deps handling around ninja -t recompact.

Poem

"I nibble backups, soft and neat,
I copy deps before they meet,
If recompact snaps, I'll hop and mend,
A broken marker spares repeat — my friend." 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 10.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: implementing a snapshot+restore mechanism with a broken-marker for handling recompact crashes, which directly aligns with the core modifications in both files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/2873-recompact-snapshot-restore

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Jun 6, 2026

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@ci/meson/build_config.py`:
- Around line 409-415: The try/except around the file operations for
deps_bak/deps_file is swallowing OSError and allowing the function to return
True even when snapshot/restore failed; change the error handling so
file-operation failures are not silent: catch OSError as e (or remove the bare
except), and either raise a new descriptive RuntimeError (including e and the
paths deps_file and deps_bak) or return False immediately so callers know the
snapshot/restore failed; ensure both the snapshot path (where snapshot_taken is
set) and the restore path propagate errors instead of continuing, and update any
callers if you switch to raising an exception so the build fails fast when
.ninja_deps cannot be copied or restored.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 0927b9f2-51c1-40c6-a6a7-e4e29f327adc

📥 Commits

Reviewing files that changed from the base of the PR and between a1311af and 9c6429c.

📒 Files selected for processing (2)
  • ci/meson/build_config.py
  • ci/tests/test_ninja_maintenance.py

Comment thread ci/meson/build_config.py Resolved
CodeRabbit flagged that the OSError handlers around snapshot and
restore silently swallowed failures, allowing the function to return
True even when the byte-preservation guarantee couldn't be honored.
Worst case on the restore path: unlink succeeded but rename failed,
leaving the build dir with no .ninja_deps at all.

Two targeted fixes that keep the "maintenance is non-fatal" contract:

  1. Snapshot failure path: log a visible warning, set the broken
     marker, and skip recompact entirely. Without a rollback path
     it's safer to leave the existing .ninja_deps in place than to
     run a tool we know can crash mid-write on this host. Calls site
     never sees an exception — the deps log is preserved.

  2. Restore failure path: replace the unlink+rename pair with a
     single atomic os.replace(deps_bak, deps_file). On POSIX and
     Windows alike, os.replace is atomic: either the target ends up
     with the snapshot's content, or nothing changes. The bad
     interleaving (target unlinked, source rename failed) is no
     longer reachable. On the unlikely failure case we now log a
     visible warning rather than swallowing OSError.

Note on CodeRabbit's specific suggestion (raise RuntimeError on
either failure): rejected — the call sites in ci/util/meson_example_runner.py
and ci/meson/runner.py treat this function as best-effort, and the
existing top-level `except Exception` handler explicitly documents
the maintenance step as non-fatal. Failing the whole build because a
.bak file couldn't be deleted would be a worse outcome than the
silent return. The visible-warning + skip-and-mark-broken pattern
above satisfies the actual concern (no silent data loss) without
breaking the non-fatal contract.

Tests: adds test_snapshot_failure_skips_recompact_and_flags_broken,
which monkey-patches shutil.copy2 to raise OSError and asserts that
RunningProcess is never invoked, .ninja_deps stays byte-identical,
and the broken marker is set. Other tests still pass — the os.replace
swap is semantically equivalent to the prior unlink+rename pair under
non-faulty filesystems.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@zackees
zackees merged commit 46da9d0 into master Jun 6, 2026
11 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 6, 2026
@zackees
zackees deleted the fix/2873-recompact-snapshot-restore branch June 6, 2026 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

1 participant