revert(sqlite): keep crash-recovery failures non-destructive; drop the rebuild fallback

This commit is contained in:
Neil
2026-11-08 21:08:42 -08:00
parent ef74ed4570
commit ed44076b28
2 changed files with 1 additions and 49 deletions
@@ -9,7 +9,6 @@ import {
writeFileSync,
writeSync
} from 'node:fs'
import { spawnSync } from 'node:child_process'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
@@ -165,41 +164,4 @@ describe('keyed JSON row cache', () => {
new KeyedJsonRowCache(path, { schemaVersion: 1, holdConnection: false }).write([row('b', '2')])
expect(restore(new KeyedJsonRowCache(path, { schemaVersion: 1 }))).toEqual(['2'])
})
it('rebuilds a cache a crashed rollback-journal writer left behind and saves again', () => {
const { path } = paths()
new KeyedJsonRowCache(path, {
schemaVersion: 1,
holdConnection: false
}).write([row('a', '1')])
// Leave a hot journal, which a read-only connection cannot roll back.
const crashed = spawnSync(process.execPath, [
'-e',
`const { DatabaseSync } = require('node:sqlite')
const db = new DatabaseSync(process.argv[1])
db.exec('PRAGMA journal_mode = DELETE')
db.exec('PRAGMA cache_size = 1')
db.exec('BEGIN IMMEDIATE')
// Spill pages to the database file so the journal is hot, not merely present.
db.exec("UPDATE rows SET value = '\\"lost\\"'")
db.exec(\`WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 2000)
INSERT INTO rows SELECT 'k' || i, i + 1, hex(randomblob(200)) FROM n\`)
process.kill(process.pid, 'SIGKILL')`,
path
])
// Windows terminates the child with an exit code instead of reporting a signal.
if (process.platform === 'win32') {
expect(crashed.status).not.toBe(0)
} else {
expect(crashed.signal).toBe('SIGKILL')
}
expect(existsSync(`${path}-journal`)).toBe(true)
const cache = new KeyedJsonRowCache(path, { schemaVersion: 1 })
expect(restore(cache)).toEqual([])
expect(existsSync(`${path}-journal`)).toBe(false)
cache.write([row('b', '2')])
cache.close()
expect(restore(new KeyedJsonRowCache(path, { schemaVersion: 1 }))).toEqual(['2'])
})
})
@@ -157,10 +157,7 @@ export class KeyedJsonRowCache {
rollback(db)
}
} catch (error) {
// A crash mid-save can leave a journal or WAL only a writable connection can recover.
// Replaying it here would run unbounded I/O on the caller's thread, so rebuild instead,
// as for corruption; otherwise every later read-only load would fail the same way.
if (classifyRowCacheFailure(error) !== 'corrupt' && !needsWritableRecovery(error)) {
if (classifyRowCacheFailure(error) !== 'corrupt') {
return { kind: 'unavailable', error }
}
db?.close()
@@ -353,10 +350,3 @@ function receiptNames(stored: unknown, receipt: string): boolean {
}
return stored === receipt || /^\d+:\d+:([0-9a-f]{64})$/.exec(stored)?.[1] === receipt
}
/** SQLITE_READONLY_RECOVERY and SQLITE_READONLY_ROLLBACK: recovery needs a writable connection. */
function needsWritableRecovery(error: unknown): boolean {
const errcode =
error && typeof error === 'object' && 'errcode' in error ? error.errcode : undefined
return errcode === 264 || errcode === 776
}