-
Notifications
You must be signed in to change notification settings - Fork 389
Add data directory locking to NodeFS to prevent multi-process corruption #892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
reisepass
wants to merge
4
commits into
electric-sql:main
Choose a base branch
from
reisepass:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+146
−0
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
23d3f30
Add PGlite crash safety tests that expose corruption bugs
reisepass 92653eb
Fix PGlite crash safety: PID file lock and partial initdb detection
reisepass cb58e9a
Simplify to single lock test, improve error message, drop extra crash…
reisepass 60fc736
Via PR comment concerns over pids can be reused we decided to remove …
reisepass File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@electric-sql/pglite': patch | ||
| --- | ||
|
|
||
| Add data directory locking to NodeFS to prevent multi-process corruption |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { describe, it, expect, afterAll } from 'vitest' | ||
| import { existsSync, writeFileSync, rmSync } from 'node:fs' | ||
|
|
||
| const dataDir = `/tmp/pglite-lock-test-${Date.now()}` | ||
|
|
||
| afterAll(async () => { | ||
| if (!process.env.RETAIN_DATA) { | ||
| for (const p of [dataDir, dataDir + '.lock']) { | ||
| if (existsSync(p)) rmSync(p, { recursive: true, force: true }) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| describe('NodeFS data directory locking', () => { | ||
| it('should block a second instance from opening the same data directory', async () => { | ||
| const { PGlite } = await import('../dist/index.js') | ||
|
|
||
| const db1 = new PGlite(dataDir) | ||
| await db1.waitReady | ||
|
|
||
| // Lock file should exist while db1 is open | ||
| expect(existsSync(dataDir + '.lock')).toBe(true) | ||
|
|
||
| // Second instance on same dir must throw | ||
| let lockError = null | ||
| try { | ||
| const db2 = new PGlite(dataDir) | ||
| await db2.waitReady | ||
| await db2.close() | ||
| } catch (err) { | ||
| lockError = err | ||
| } | ||
|
|
||
| expect(lockError).not.toBeNull() | ||
| expect(lockError.message).toContain('already in use') | ||
| expect(lockError.message).toContain(String(process.pid)) | ||
|
|
||
| // First instance should still work fine | ||
| const result = await db1.query('SELECT 1 as ok') | ||
| expect(result.rows[0].ok).toBe(1) | ||
|
|
||
| await db1.close() | ||
| }, 30000) | ||
|
|
||
| it('should allow reopening after the first instance is closed', async () => { | ||
| const { PGlite } = await import('../dist/index.js') | ||
|
|
||
| // Lock file should be cleaned up after close | ||
| expect(existsSync(dataDir + '.lock')).toBe(false) | ||
|
|
||
| const db = new PGlite(dataDir) | ||
| await db.waitReady | ||
| const result = await db.query('SELECT 1 as ok') | ||
| expect(result.rows[0].ok).toBe(1) | ||
| await db.close() | ||
| }, 30000) | ||
|
|
||
| it('should override a stale lock from a dead process', async () => { | ||
| const { PGlite } = await import('../dist/index.js') | ||
|
|
||
| // Write a fake lock file with a PID that doesn't exist | ||
| writeFileSync(dataDir + '.lock', '999999\n0\n') | ||
|
|
||
| // Should succeed — stale lock gets overridden | ||
| const db = new PGlite(dataDir) | ||
| await db.waitReady | ||
| const result = await db.query('SELECT 1 as ok') | ||
| expect(result.rows[0].ok).toBe(1) | ||
| await db.close() | ||
| }, 30000) | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.