GeezerVison is showing github.com — fix(core): retry sqlite lock timeouts only outside transactions · anomalyco/opencode@54f5929
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/database/sqlite.bun.ts +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,6 +55,7 @@ const make = (options: Config) => | 55 | 55 | ? Statement.defaultTransforms(options.transformResultNames).array | 56 | 56 | : undefined | 57 | 57 |
| | 58 | + const retryIfAutocommit = retryLocked(() => native.inTransaction) | 58 | 59 | const run = (query: string, params: ReadonlyArray<unknown> = []) => | 59 | 60 | Effect.withFiber<Array<Record<string, unknown>>, SqlError>((fiber) => { | 60 | 61 | const statement = native.query(query) | @@ -69,7 +70,7 @@ const make = (options: Config) => | 69 | 70 | }), | 70 | 71 | ) | 71 | 72 | } | 72 | | - }).pipe(retryLocked) | | 73 | + }).pipe(retryIfAutocommit) | 73 | 74 |
| 74 | 75 | const runValues = (query: string, params: ReadonlyArray<unknown> = []) => | 75 | 76 | Effect.withFiber<Array<unknown[]>, SqlError>((fiber) => { | @@ -85,7 +86,7 @@ const make = (options: Config) => | 85 | 86 | }), | 86 | 87 | ) | 87 | 88 | } | 88 | | - }).pipe(retryLocked) | | 89 | + }).pipe(retryIfAutocommit) | 89 | 90 |
| 90 | 91 | const connection = identity<SqliteConnection>({ | 91 | 92 | execute(query, params, transformRows) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/database/sqlite.node.ts +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,6 +54,7 @@ const make = (options: Config) => | 54 | 54 | ? Statement.defaultTransforms(options.transformResultNames).array | 55 | 55 | : undefined | 56 | 56 |
| | 57 | + const retryIfAutocommit = retryLocked(() => native.isTransaction) | 57 | 58 | const run = (query: string, params: ReadonlyArray<unknown> = []) => | 58 | 59 | Effect.withFiber<Array<Record<string, unknown>>, SqlError>((fiber) => { | 59 | 60 | const statement = native.prepare(query) | @@ -67,7 +68,7 @@ const make = (options: Config) => | 67 | 68 | }), | 68 | 69 | ) | 69 | 70 | } | 70 | | - }).pipe(retryLocked) | | 71 | + }).pipe(retryIfAutocommit) | 71 | 72 |
| 72 | 73 | const runValues = (query: string, params: ReadonlyArray<unknown> = []) => | 73 | 74 | Effect.withFiber<ReadonlyArray<ReadonlyArray<unknown>>, SqlError>((fiber) => { | @@ -85,7 +86,7 @@ const make = (options: Config) => | 85 | 86 | }), | 86 | 87 | ) | 87 | 88 | } | 88 | | - }).pipe(retryLocked) | | 89 | + }).pipe(retryIfAutocommit) | 89 | 90 |
| 90 | 91 | const connection = identity<SqliteConnection>({ | 91 | 92 | execute(query, params, transformRows) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/database/sqlite.ts +16-11Lines changed: 16 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,14 +13,19 @@ export class Drizzle extends Context.Service<Drizzle, DrizzleClient>()("@opencod | 13 | 13 | // statement itself is fine — retrying with an async sleep lets the competing | 14 | 14 | // process finish so the next attempt can acquire the lock. Without this, one | 15 | 15 | // long write in a concurrent process kills another process's prompt. | 16 | | -export const retryLocked = <A, E>(effect: Effect.Effect<A, E, never>) => | 17 | | - effect.pipe( | 18 | | - Effect.retry({ | 19 | | - while: (error) => error instanceof SqlError && error.reason._tag === "LockTimeoutError", | 20 | | - schedule: Schedule.exponential(50, 2).pipe( | 21 | | - Schedule.either(Schedule.spaced(250)), | 22 | | - Schedule.jittered, | 23 | | - Schedule.while((meta) => meta.elapsed < 30_000), | 24 | | - ), | 25 | | - }), | 26 | | - ) | | 16 | +// Only autocommit statements are retried. Inside a transaction the same error repeats | | 17 | +// until the transaction is rolled back and restarted (e.g. SQLITE_BUSY_SNAPSHOT on a | | 18 | +// deferred transaction whose read snapshot went stale), so it is surfaced at once. | | 19 | +export const retryLocked = | | 20 | + (inTransaction: () => boolean) => | | 21 | + <A, E>(effect: Effect.Effect<A, E, never>) => | | 22 | + effect.pipe( | | 23 | + Effect.retry({ | | 24 | + while: (error) => error instanceof SqlError && error.reason._tag === "LockTimeoutError" && !inTransaction(), | | 25 | + schedule: Schedule.exponential(50, 2).pipe( | | 26 | + Schedule.either(Schedule.spaced(250)), | | 27 | + Schedule.jittered, | | 28 | + Schedule.while((meta) => meta.elapsed < 30_000), | | 29 | + ), | | 30 | + }), | | 31 | + ) |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/sqlite-lock-retry.test.ts +76Lines changed: 76 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,3 +44,79 @@ test("retries statements while another process holds the write lock", async () = | 44 | 44 | await rm(dir, { recursive: true, force: true }).catch(() => {}) | 45 | 45 | } | 46 | 46 | }) | | 47 | + | | 48 | +// Regression: a DEFERRED WAL transaction that holds a read snapshot cannot be | | 49 | +// upgraded to a write after another connection commits — the snapshot is | | 50 | +// stale and the upgrade fails with SQLITE_BUSY_SNAPSHOT. #47567's retry | | 51 | +// wrapper, applied indiscriminately, would loop this error for the full | | 52 | +// 30 s budget; only a rollback + restart of the whole transaction helps. | | 53 | +// Inside an explicit transaction, statements must fail fast. | | 54 | +test("fails fast on BUSY_SNAPSHOT inside a deferred transaction", async () => { | | 55 | + const dir = await mkdtemp(join(tmpdir(), "sqlite-snapshot-")) | | 56 | + const filename = join(dir, "snap.db") | | 57 | + | | 58 | + try { | | 59 | + // Set up schema and one row, enable WAL, all via the layer. | | 60 | + await Effect.runPromise( | | 61 | + Effect.gen(function* () { | | 62 | + const db = yield* EffectDrizzleSqlite.makeWithDefaults() | | 63 | + const native = (yield* Sqlite.Native) as Database | | 64 | + native.run("create table t (id integer primary key, v text)") | | 65 | + native.run("insert into t values (1, 'before')") | | 66 | + native.run("PRAGMA journal_mode = WAL") | | 67 | + native.run("PRAGMA busy_timeout = 0") | | 68 | + }).pipe(Effect.provide(sqliteLayer({ filename })), Effect.scoped), | | 69 | + ) | | 70 | + | | 71 | + // Inside a deferred transaction: read, then have another connection write | | 72 | + // and commit (invalidating the read snapshot), then attempt a write. | | 73 | + // The write must surface a LockTimeoutError immediately, not loop the | | 74 | + // 30 s retry budget. | | 75 | + const start = Date.now() | | 76 | + let error: unknown | | 77 | + try { | | 78 | + await Effect.runPromise( | | 79 | + Effect.gen(function* () { | | 80 | + const db = yield* EffectDrizzleSqlite.makeWithDefaults() | | 81 | + yield* db.transaction( | | 82 | + (tx) => | | 83 | + Effect.gen(function* () { | | 84 | + yield* tx.get(sql`select v from t where id = 1`) | | 85 | + // A separate connection writes and commits while we hold the | | 86 | + // deferred transaction's read snapshot. | | 87 | + yield* Effect.sync(() => { | | 88 | + const other = new Database(filename) | | 89 | + other.run("update t set v = 'after' where id = 1") | | 90 | + other.close() | | 91 | + }) | | 92 | + // This write must fail with BUSY_SNAPSHOT and surface fast — | | 93 | + // the retry helper must NOT keep trying the same stale snapshot. | | 94 | + yield* tx.run(sql`update t set v = 'after2' where id = 1`) | | 95 | + }), | | 96 | + { behavior: "deferred" }, | | 97 | + ) | | 98 | + }).pipe(Effect.provide(sqliteLayer({ filename })), Effect.scoped), | | 99 | + ) | | 100 | + } catch (e) { | | 101 | + error = e | | 102 | + } | | 103 | + const elapsed = Date.now() - start | | 104 | + | | 105 | + expect(error).toBeDefined() | | 106 | + // drizzle wraps the raw SqlError inside EffectDrizzleQueryError.cause. | | 107 | + // Effect wraps the SqlError in a Cause. Walk the failure chain to confirm | | 108 | + // the surface error is a LockTimeoutError wrapping SQLITE_BUSY_SNAPSHOT. | | 109 | + const reasons = | | 110 | + ( | | 111 | + error as { | | 112 | + cause?: { reasons?: ReadonlyArray<{ error?: { reason?: { _tag?: string; cause?: { code?: string } } } }> } | | 113 | + } | | 114 | + ).cause?.reasons ?? [] | | 115 | + const inner = reasons[0]?.error | | 116 | + expect(inner?.reason?._tag).toBe("LockTimeoutError") | | 117 | + expect((inner?.reason?.cause as { code?: string } | undefined)?.code).toBe("SQLITE_BUSY_SNAPSHOT") | | 118 | + expect(elapsed).toBeLessThan(2000) | | 119 | + } finally { | | 120 | + await rm(dir, { recursive: true, force: true }).catch(() => {}) | | 121 | + } | | 122 | +}) |
|