fix: add file-based lock to prevent PGLite concurrent access crashes (Aborted())#61
Closed
danbr wants to merge 1 commit into
Closed
fix: add file-based lock to prevent PGLite concurrent access crashes (Aborted())#61danbr wants to merge 1 commit into
danbr wants to merge 1 commit into
Conversation
PGLite uses embedded Postgres (WASM) which only supports one connection at a time. When `gbrain embed` is running (which can take minutes with 700+ pages) and another process tries to connect (e.g. `gbrain query`), PGLite throws `Aborted()` because it can't handle concurrent access to the same data directory. This patch adds a file-based advisory lock using atomic `mkdir` with PID tracking for stale lock detection: - `acquireLock()` creates `.gbrain-lock/` dir with a lock file containing PID, timestamp, and command - Stale locks from dead processes are auto-cleaned - In-memory PGLite (undefined dataDir) skips locking entirely since it's process-scoped - `disconnect()` releases the lock via `releaseLock()` - Default 30s timeout with clear error message showing which process holds the lock and how to recover Tested: all 348 existing tests pass + 6 new lock tests.
8 tasks
Owner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When
gbrain embedis running (which can take minutes with 700+ pages) and another process tries to connect to the same PGLite database (e.g.gbrain query), PGLite throwsAborted()because embedded Postgres (WASM) only supports one connection at a time. This is a hard crash — no recovery, no helpful error.Root Cause
PGLite uses a single WASM Postgres instance bound to a filesystem directory. Concurrent access from multiple processes to the same data directory causes an assertion failure in the Postgres WAL layer, producing
Aborted()with no useful context.Fix
Added a file-based advisory lock (
src/core/pglite-lock.ts) that:mkdirwhich is POSIX-atomic, so no race conditions between processes.gbrain-lock/lockinside the data directory, so you can see exactly what process holds the lockundefineddataDir means process-scoped, so no concurrent access is possiblerm -rf .gbrain-lock)PGLiteEngine.disconnect()callsreleaseLock()in a finally-like patternTesting
test/pglite-lock.test.ts: acquire/release, concurrent prevention, stale detection, in-memory skip, PID tracking, cleanup on disconnectAborted()crash no longer occurs; concurrent processes get a clear error message insteadFiles Changed
src/core/pglite-lock.tssrc/core/pglite-engine.tsacquireLock/releaseLockinconnect/disconnecttest/pglite-lock.test.ts