Skip to content

fix: serialize local PGLite access and recover stale locks#60

Closed
k0nkupa wants to merge 1 commit into
garrytan:masterfrom
k0nkupa:fix/pglite-locking-recovery
Closed

fix: serialize local PGLite access and recover stale locks#60
k0nkupa wants to merge 1 commit into
garrytan:masterfrom
k0nkupa:fix/pglite-locking-recovery

Conversation

@k0nkupa

@k0nkupa k0nkupa commented Apr 12, 2026

Copy link
Copy Markdown

Summary

  • add a file lock for file-backed PGLite brains so only one local gbrain process can open the same brain at a time
  • fail fast with a clear error when the brain is already in use instead of letting PGLite crash obscurely
  • recover stale .gbrain.lock files and stale postmaster.pid files when the recorded pid is no longer alive
  • add focused tests for concurrent file-backed access and stale lock recovery

Why

Local PGLite brains are single-user, but the CLI currently allows multiple commands to open the same file-backed brain concurrently. In practice this can leave the brain wedged after interrupted imports or overlapping commands, with errors like PGLite failed to initialize properly instead of an actionable message.

This keeps the local path honest: serialize access, recover obvious stale state, and tell the user what to do.

Test plan

  • bun test test/pglite-engine.test.ts
  • verified new tests:
    • rejects a second connection to the same file-backed brain
    • recovers from stale .gbrain.lock and stale postmaster.pid

Notes

  • this is intentionally scoped to local file-backed PGLite only
  • in-memory PGLite behavior is unchanged
  • repo-wide TypeScript still has unrelated pre-existing errors outside this patch

@garrytan

Copy link
Copy Markdown
Owner

Closing as duplicate of #61 — both fix the same PGLite concurrent access crash. We went with #61's separate-module approach (pglite-lock.ts) for better testability and separation of concerns. Your postmaster.pid cleanup idea is noted for a potential follow-up. Thanks for the contribution! 🙏

@garrytan garrytan closed this Apr 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants