|
| 1 | +import { Effect, Semaphore } from "effect" |
| 2 | +import { openSetupLock } from "#sqlite" |
| 3 | + |
| 4 | +/** |
| 5 | + * Cross-process mutex for first-run database setup (WAL switch and migrations). |
| 6 | + * |
| 7 | + * SQLite cannot queue a deferred read-to-write upgrade, so concurrent fresh starts failed at once with |
| 8 | + * "database is locked". An exclusive transaction on a sentinel database next to the real one serializes |
| 9 | + * them, and the OS releases it if the holder dies. |
| 10 | + * |
| 11 | + * `acquireSync` blocks the thread, so it is only for sections that never yield. A section that yields |
| 12 | + * (the effect-based migration) uses `hold`, which polls without blocking. A synchronous caller in the |
| 13 | + * same process as an active `hold` skips the lock: blocking would deadlock the holder, and connections |
| 14 | + * in one process already queue through their own busy handlers. |
| 15 | + */ |
| 16 | +const semaphores = new Map<string, Semaphore.Semaphore>() |
| 17 | +const active = new Set<string>() |
| 18 | + |
| 19 | +const sentinel = (filename: string) => filename + ".setup-lock" |
| 20 | +const SYNC_TIMEOUT_MS = 60_000 |
| 21 | +const POLL_MS = 25 |
| 22 | +const HOLD_TIMEOUT_MS = 60_000 |
| 23 | + |
| 24 | +export function acquireSync(filename: string): () => void { |
| 25 | + if (filename === ":memory:" || active.has(filename)) return () => {} |
| 26 | + const lock = openSetupLock(sentinel(filename), SYNC_TIMEOUT_MS) |
| 27 | + try { |
| 28 | + lock.exec("BEGIN EXCLUSIVE") |
| 29 | + } catch (error) { |
| 30 | + lock.close() |
| 31 | + throw error |
| 32 | + } |
| 33 | + return () => { |
| 34 | + try { |
| 35 | + lock.exec("ROLLBACK") |
| 36 | + } finally { |
| 37 | + lock.close() |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function isBusy(error: unknown) { |
| 43 | + return /locked|busy/i.test(error instanceof Error ? error.message : String(error)) |
| 44 | +} |
| 45 | + |
| 46 | +/** Runs `body` holding the lock; waiting is async so in-process peers keep making progress. */ |
| 47 | +export function hold<A, E, R>(filename: string, body: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> { |
| 48 | + if (filename === ":memory:") return body |
| 49 | + let semaphore = semaphores.get(filename) |
| 50 | + if (!semaphore) { |
| 51 | + semaphore = Semaphore.makeUnsafe(1) |
| 52 | + semaphores.set(filename, semaphore) |
| 53 | + } |
| 54 | + const acquire = Effect.gen(function* () { |
| 55 | + const started = Date.now() |
| 56 | + while (true) { |
| 57 | + const lock = openSetupLock(sentinel(filename), 0) |
| 58 | + const result = yield* Effect.try({ try: () => lock.exec("BEGIN EXCLUSIVE"), catch: (error) => error }).pipe( |
| 59 | + Effect.map(() => lock), |
| 60 | + Effect.catch((error) => |
| 61 | + Effect.sync(() => lock.close()).pipe( |
| 62 | + Effect.flatMap(() => |
| 63 | + isBusy(error) && Date.now() - started < HOLD_TIMEOUT_MS ? Effect.succeed(undefined) : Effect.die(error), |
| 64 | + ), |
| 65 | + ), |
| 66 | + ), |
| 67 | + ) |
| 68 | + if (result) { |
| 69 | + active.add(filename) |
| 70 | + return result |
| 71 | + } |
| 72 | + yield* Effect.sleep(POLL_MS) |
| 73 | + } |
| 74 | + }) |
| 75 | + const release = (lock: { exec: (sql: string) => void; close: () => void }) => |
| 76 | + Effect.sync(() => { |
| 77 | + active.delete(filename) |
| 78 | + try { |
| 79 | + lock.exec("ROLLBACK") |
| 80 | + } finally { |
| 81 | + lock.close() |
| 82 | + } |
| 83 | + }) |
| 84 | + return semaphore.withPermits(1)(Effect.acquireUseRelease(acquire, () => body, release)) |
| 85 | +} |
0 commit comments