fix(storage): flush before acking in local write_text_if_absent#204
Merged
Conversation
tokio's async File buffers writes internally: write_all only fills the buffer, and the actual OS write happens in a background task after drop — so write_text_if_absent could return Ok(true) with the file created but still EMPTY, and an immediate reader saw EOF. Caught twice in CI as 'EOF while parsing a value' reading state.json right after cluster import (the cluster's first state-write routes here since the storage port); also an invariant-6 violation (acknowledged before the write reached the OS). The other local write paths use tokio::fs::write, which flushes internally — this was the one miss. Fix: flush().await before Ok, with the same remove-on-failure cleanup as the write itself. Regression test is a best-effort tight loop (the window is timing-dependent; the two CI failures are the recorded red) asserting read-after-ack never sees a short file. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
aaltshuler has reached the 50-review limit for trial accounts. To continue receiving code reviews, upgrade your plan.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The "flaky" cluster test that failed CI twice (#199's first run, #202) was a real bug, not a flake.
Root cause:
LocalStorageAdapter::write_text_if_absent(the create-only CAS used for the cluster's first state-write, the lock, and sidecars since #190) writes through tokio's asyncFilewithwrite_allbut never flushes. tokio files buffer internally — the OS write happens in a background task after the function returns — soOk(true)could be acknowledged with the file created but empty, and an immediate reader sawEOF while parsing. Invariant 6 (durable before acknowledgement) violated on exactly one path: the other local writes usetokio::fs::write, which flushes internally.Fix:
flush().awaitbeforeOk(true), with the same remove-on-failure cleanup. One real line.Test: best-effort tight-loop regression (
write_text_if_absent_is_read_consistent_immediately) — the window is timing-dependent and reproduces reliably only on loaded runners, so the two recorded CI failures are the red half of the red→green pair; the commit message says so explicitly rather than pretending local determinism.Full workspace gate green (57 suites). Unblocks #202 (the version bump), whose only failure was this.
🤖 Generated with Claude Code