{"record":{"id":"5af5cc1af70db024","repo":"benbjohnson/litestream","slug":"write-ltx-from-db-w","errorCode":null,"errorMessage":"write ltx from db: %w","messagePattern":"write ltx from db: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"db.go","lineNumber":2179,"sourceCode":"\t\t}\n\t\treturn result, fmt.Errorf(\"encode ltx header: %w\", err)\n\t}\n\n\t// If we need a full snapshot, then copy from the database & WAL.\n\t// Otherwise, just copy incrementally from the WAL.\n\tif info.snapshotting {\n\t\tdb.setSyncDiagPhase(diagPhaseWriteLTXFromDB,\n\t\t\tfunc(s *diagState) {\n\t\t\t\ts.txID = txID\n\t\t\t\ts.walSize = sz\n\t\t\t\ts.snapshotting = true\n\t\t\t\ts.reason = info.reason\n\t\t\t})\n\t\tif err := db.writeLTXFromDB(ctx, enc, walFile, commit, pageMap); err != nil {\n\t\t\tif isDiskFullError(err) {\n\t\t\t\treturn result, NewLTXError(\"stage-write\", tmpFilename, 0, uint64(txID), uint64(txID), fmt.Errorf(\"%w: %w\", ErrDiskFull, err))\n\t\t\t}\n\t\t\treturn result, fmt.Errorf(\"write ltx from db: %w\", err)\n\t\t}\n\t} else {\n\t\tdb.setSyncDiagPhase(diagPhaseWriteLTXFromWAL,\n\t\t\tfunc(s *diagState) {\n\t\t\t\ts.txID = txID\n\t\t\t\ts.walSize = sz\n\t\t\t\ts.snapshotting = false\n\t\t\t\ts.reason = info.reason\n\t\t\t})\n\t\tif err := db.writeLTXFromWAL(ctx, enc, walFile, info.prevCommit, commit, pageMap); err != nil {\n\t\t\tif isDiskFullError(err) {\n\t\t\t\treturn result, NewLTXError(\"stage-write\", tmpFilename, 0, uint64(txID), uint64(txID), fmt.Errorf(\"%w: %w\", ErrDiskFull, err))\n\t\t\t}\n\t\t\treturn result, fmt.Errorf(\"write ltx from wal: %w\", err)\n\t\t}\n\t}\n\n\t// Encode final trailer to the end of the LTX file.","sourceCodeStart":2161,"sourceCodeEnd":2197,"githubUrl":"https://github.com/benbjohnson/litestream/blob/4ed7a308f6271ebfd2b0a6e4b70b03011a37e4a3/db.go#L2161-L2197","documentation":"db.writeLTXFromDB failed with a non-disk-full error while copying pages from the database file and WAL into the snapshot LTX. This is the page-copy phase of snapshot replication; failures here abort the snapshot and the staged temp file is removed by the deferred os.Remove.","triggerScenarios":"Read errors on the database file or WAL file during page iteration (I/O error, file truncated/corrupted mid-copy), context cancellation from the caller, or an ltx encoder error propagating from page writes.","commonSituations":"Corrupt or externally modified DB/WAL files (another tool touching the SQLite files); storage failures on the DB volume; litestream shutdown mid-snapshot causing context cancellation.","solutions":["Verify DB integrity: run `sqlite3 <db> 'PRAGMA integrity_check;'` and check the WAL file is intact.","Ensure only litestream manages the database files — stop tools that copy/rotate the db or WAL.","Check dmesg for read I/O errors on the database volume.","Re-run replication; if local LTX state is broken, `litestream reset` for the database.","Ensure litestream is not being stopped/restarted mid-snapshot (init system restart loops)."],"exampleFix":"// before: nightly rsync job copies db + wal while litestream runs\n0 * * * * rsync -a /data/app.db /backup/\n// after: use sqlite backup or litestream restore instead\n0 * * * * litestream restore -o /backup/app.db /data/app.db","handlingStrategy":"try-catch","validationCode":"if err := sqliteIntegrityCheck(dbPath); err != nil {\n    return fmt.Errorf(\"db corrupt, fix before replication: %w\", err)\n}\n// integrity_check via sqlite3 CLI: sqlite3 db 'PRAGMA integrity_check;'","typeGuard":null,"tryCatchPattern":"var le *os.PathError\nif errors.As(err, &le) && errors.Is(le.Err, syscall.EIO) {\n    log.Printf(\"read I/O error on %s during snapshot: %v\", le.Path, le.Err)\n    // check hardware, restore from backup, then reset\n}","preventionTips":["Never let other tools read-modify or copy live db/WAL files; use litestream restore for copies","Run periodic PRAGMA integrity_check","Avoid restarting litestream mid-snapshot; use graceful shutdown","Monitor storage health on the DB volume"],"tags":["ltx","snapshot","io-error","replication"],"backgroundTag":"file-read-failed","analyzedSha":"4ed7a308f6271ebfd2b0a6e4b70b03011a37e4a3","analyzedAt":"2026-09-06T18:29:25.564Z","contentChangedAt":"2026-09-06T18:29:25.564Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}