← Back to Pull requests

fix(memiavl): bound WAL catch-up waits and latch sync WAL write errors #150

Closed
unknown closed this pull request without merging on Oct 28, 2026 14:50 UTC
View on GitHub
unknown unknown commented on Oct 23, 2026 17:10 UTC
Author

What

memiavl's WAL readers (TraverseStateChanges, FirstStoreVersions) and the Commit-path snapshot switch share one bounded wait for the WAL to catch up. A failed sync WAL write is now latched like an async writer death.

Issue

Commit bumps the version before it writes the WAL. If that write fails, the WAL never reaches the version:

  • Readers waiting for it hung forever. This happened in async mode (dead writer) and sync mode (async-commit-buffer = -1).

  • An overflowing MultiTree.SaveVersion also bumped commit info without a WAL write.

  • Right after a reload, the snapshot can be ahead of the WAL. The old wait treated the snapshot as caught up, so readers returned early and could miss the latest version.
  • Solution

  • One latch: walErr is an atomic error, set by the async writer or by a failed sync write (errAsyncWriterQuit, errSyncWALWrite).

  • - Commit checks it first, so it fails before saving a new version.
    - Waiting readers read it without db.mtx and fail at once.
    - walQuit is now just closed when the writer exits.
  • One wait loop (waitForWAL):

  • - It checks the latch before every WAL read.
    - It times out after a 5s catch-up timeout. The timeout restarts whenever the WAL advances, so only a stalled WAL times out.
    - The snapshot version counts only while the WAL is empty.
    - The Commit path keeps its 50ns poll, since it waits under db.mtx. Readers poll every 1ms with no lock held.
  • Shorter reader waits:

  • - Both readers skip the wait on a read-only DB, whose WAL can't advance.
    - TraverseStateChanges waits only up to its endVersion, so an old range doesn't wait on later commits. It skips the wait for a reversed range.
    - Targets below initialVersion skip the wait.
  • Overflow: MultiTree.SaveVersion checks every tree for uint32 overflow before bumping anything. A failed save no longer leaves commit info, and the trees saved before it, already bumped.

  • Behavior changes:

  • - Readers fail on a stalled WAL or a latched error instead of hanging.
    - FirstStoreVersions(nil) returns the latched error.
    - After a failed sync write, WaitAsyncCommit and Close return it, as in async mode.
  • Limitation: a write that hangs inside wal.Log holds the lock LastIndex needs, so the timeout can't bound it. This was the same on master.

  • Tests: WALWaitTestSuite now covers every WAL wait and WAL error case. It absorbs six older tests (TestWaitCommittedVersion, TestCommitAbortsOnAsyncWALWriteError, TestSnapshotRewriteWaitAbortsOnAsyncWALError). I checked each case by breaking the behavior it names.
  • Diff: 328 lines (+246 / -82) across 4 files Reviewer: thomas-nguy Review duration: 7060m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 328 lines diff across 4 files
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.347
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #150"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.1% (MEDIUM) · Higher = larger blast radius
    42.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.6% · Safe to merge
    16.6%