← Back to Pull requests

Fix c1z corruption from incomplete WAL checkpoint before saveC1z #675

Closed
unknown closed this pull request without merging on Feb 10, 2026 17:46 UTC
View on GitHub
unknown unknown commented on Feb 10, 2026 16:04 UTC
Author

Summary

Fixes intermittent c1z corruption where saveC1z() compresses an incomplete SQLite database because the WAL checkpoint before compression silently fails to move all frames into the main db file.

Root cause

saveC1z() reads the main .db file via raw os.Open() — it does not read the -wal file. If the WAL checkpoint didn't move all frames into the main db, the resulting c1z is missing data. Three compounding issues made this likely:

1. Unbounded connection pool blocks checkpoint: NewC1File never called SetMaxOpenConns, so sql.DB could hold multiple idle connections. In WAL mode, each idle connection holds a read lock that prevents PRAGMA wal_checkpoint(TRUNCATE) from completing all frames. In theory we only make one connection, but let's make sure.

2. Partial checkpoint undetectable: The checkpoint used ExecContext which discards the (busy, log, checkpointed) return values. A partial checkpoint returned nil error — the code had no way to know frames were left in the WAL.

3. Hard failures swallowed: Even outright checkpoint errors (context cancellation, I/O) were logged as warnings and execution continued to saveC1z.

This manifests most on ZFS with large ARC caches because more WAL data accumulates before flush, and longer TXG intervals create bigger WAL files with more frames to checkpoint.

Changes

c1file.go:

  • Add SetMaxOpenConns(1) after sql.Open() — eliminates pool connection interference with WAL checkpoint

  • Replace ExecContext with QueryRowContext + Scan to read the (busy, log, checkpointed) result and detect partial checkpoints

  • Return errors on checkpoint failure instead of logging and continuing

  • Nil out rawDb/db on error paths to prevent use-after-close on retry
  • clone_sync.go:

  • Close the schema-init connection immediately via rawDb.Close() instead of deferring full C1File.Close()

  • Removes redundant out.init() call (already called internally by NewC1File)

  • Ensures no competing connection exists during outFile.Close() checkpoint
  • Test plan

  • CI passes

  • Verify SetMaxOpenConns(1) doesn't cause deadlocks in sync workflows (confirmed: no nested/concurrent query patterns exist in codebase)

  • Verify DELETE journal mode (non-WAL) still works — checkpoint returns (0, -1, -1), condition log >= 0 is false, skips the incomplete check

  • Monitor for WAL checkpoint failed or WAL checkpoint incomplete errors in production after deploy
  • <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    Summary by CodeRabbit

    Release Notes

    Bug Fixes
    Improved database cleanup and error handling for WAL checkpoint operations, ensuring proper resource cleanup on failures.
    Enhanced error reporting for incomplete database checkpoints.

    Chores
    * Optimized internal database connection management and resource handling to prevent contention during database operations.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 384 lines (+288 / -96) across 4 files Reviewer: loganintech Review duration: 101m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 384 lines diff across 4 files
    0.43
    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.356
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #675"
    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: 43% (MEDIUM) · Higher = larger blast radius
    43%
    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: 17.0% · Safe to merge
    17.0%