Skip to content

Commit b1fa3f1

Browse files
anandgupta42claude
andcommitted
fix(drivers): stop the driver fabricating a lock wrapper the registry then trusts
Two defects my previous commit introduced, caught in review. **The driver's lock matcher was looser than the registry's.** I tightened `categorizeConnectionError` to require both `could not set lock` and `conflicting lock`, precisely so a lock-shaped error from elsewhere could not be claimed as contention — but left `isLockError` in the driver matching `could not set lock` alone. A non-contention failure (an unsupported filesystem lock, a permissions problem) therefore entered the lock branch, spent a read-only retry, and was wrapped as "locked by another process". The registry then trusted that fabricated wrapper as a recoverable `store_locked` and told the reader to close a process that does not exist, hiding the real filesystem fault. The two matchers now agree, with a comment on each saying so. **The liveness test leaked its handle on failure.** `close()` ran after both assertions, so a failing one skipped it. Since that test opens the store read-write and DuckDB's lock is per-process, the leak would make every later test in the file fail with a lock conflict rather than the real cause — the exact confusion this suite exists to prevent. Now closed in a `finally`, with the assertions moved after it. Test: a non-contention `Could not set lock … Operation not supported` must keep its own message, must not be relabelled "locked by another process", and must not spend a retry. It fails against the previous commit's matcher. Gates: typecheck 13/13; markers ok against `origin/main`; drivers 143 pass 0 fail; `test/altimate` 4224 pass 0 fail; gated E2E 22 pass 0 fail on six consecutive runs. Lint 5865 warnings / 1 error — the error remains the pre-existing `consistent-return` in `packages/http-recorder/test/record-replay.test.ts`; +1 warning, an un-awaited `mock.module` in the new test, matching every other `mock.module` in that file. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VqnuBDGkh1ZT65Ti7e6DHZ
1 parent c06a851 commit b1fa3f1

3 files changed

Lines changed: 65 additions & 7 deletions

File tree

‎packages/drivers/src/duckdb.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,14 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
7474
const msg = (err instanceof Error ? err.message : String(err)).toLowerCase()
7575
return (
7676
msg.includes("locked") ||
77-
msg.includes("conflicting lock") ||
78-
msg.includes("could not set lock") ||
77+
// Both halves, not either. "could not set lock" on its own also covers
78+
// non-contention failures — an unsupported filesystem lock, a permissions
79+
// problem — and matching it alone would wrap those as "locked by another
80+
// process", fabricating a wrapper that Registry.categorizeConnectionError
81+
// then trusts as a recoverable `store_locked`. That would send the reader
82+
// hunting for a process to close while hiding the real filesystem fault.
83+
// Registry's raw matcher already requires both; these must agree.
84+
(msg.includes("could not set lock") && msg.includes("conflicting lock")) ||
7985
msg.includes("sqlite_busy")
8086
)
8187
}

‎packages/drivers/test/driver-security.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,50 @@ describe("DuckDB driver", () => {
189189
await connector.close()
190190
})
191191

192+
test("does not claim a non-contention lock failure as a foreign lock", async () => {
193+
// "Could not set lock" without "Conflicting lock" is a filesystem fault,
194+
// not contention. Wrapping it as "locked by another process" would send
195+
// the reader after a process to close and hide the real cause — and the
196+
// registry would trust that fabricated wrapper as recoverable.
197+
let attempts = 0
198+
mock.module("duckdb", () => ({
199+
default: {
200+
Database: class {
201+
constructor(_path: string, optsOrCb: any, cb?: (err: Error | null) => void) {
202+
attempts++
203+
setTimeout(
204+
() =>
205+
openCallback(optsOrCb, cb)(
206+
new Error('IO Error: Could not set lock on file "/tmp/test.duckdb": Operation not supported'),
207+
),
208+
0,
209+
)
210+
}
211+
connect() {
212+
return {}
213+
}
214+
close(cb: any) {
215+
if (cb) cb(null)
216+
}
217+
},
218+
},
219+
}))
220+
221+
const { connect } = await import("../src/duckdb")
222+
const connector = await connect({ type: "duckdb", path: "/tmp/test.duckdb" })
223+
224+
try {
225+
await connector.connect()
226+
expect.unreachable("Should have thrown")
227+
} catch (e: any) {
228+
expect(e.message).toContain("Operation not supported")
229+
expect(e.message).not.toContain("locked by another process")
230+
}
231+
232+
// And it is not treated as contention, so no read-only retry is spent.
233+
expect(attempts).toBe(1)
234+
})
235+
192236
test("retries with READ_ONLY when the first open fails with DuckDB's real lock text", async () => {
193237
// The other retry test drives a fabricated "DUCKDB_LOCKED: file is locked"
194238
// string, which the driver's original `.includes("locked")` check already

‎packages/opencode/test/altimate/duckdb-open-e2e.test.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,22 @@ describe("DuckDB driver: opening a real store", () => {
113113
// The assertion above this one passes on a dead connection: `(0 rows)` and
114114
// "the driver never loaded" are the same observation. This one cannot.
115115
const c = await connect({ type: "duckdb", path: storePath })
116-
await c.connect()
117-
const r = await c.execute(`SELECT md5('${LIVENESS_NONCE}') AS h`)
116+
// close() in a finally, not after the assertions: this opens `storePath`
117+
// read-write, and DuckDB's lock is per-process, so a handle leaked by a
118+
// failing assertion would make every later test in this file fail with a
119+
// lock conflict instead of the real cause.
120+
let rows: unknown[][]
121+
try {
122+
await c.connect()
123+
rows = (await c.execute(`SELECT md5('${LIVENESS_NONCE}') AS h`)).rows
124+
} finally {
125+
await c.close()
126+
}
118127
// Exactly one row: a swallowed failure surfaces as zero rows, and zero rows
119128
// is the shape that reads as success everywhere downstream.
120-
expect(r.rows.length).toBe(1)
129+
expect(rows.length).toBe(1)
121130
// And the right answer, which requires actually computing it.
122-
expect(String(r.rows[0][0])).toBe(LIVENESS_MD5)
123-
await c.close()
131+
expect(String(rows[0][0])).toBe(LIVENESS_MD5)
124132
})
125133

126134
ddbTest("opens the same store from many connectors at once", async () => {

0 commit comments

Comments
 (0)