Repository navigation
fix(store-postgres): close embedded PGlite so one-shot processes exit (fixes Packed Consumer 6h wedge) #72
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { execFileSync } from 'node:child_process' | ||
| import { mkdtempSync, writeFileSync } from 'node:fs' | ||
| import { tmpdir } from 'node:os' | ||
| import { join } from 'node:path' | ||
| import { fileURLToPath } from 'node:url' | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| // Regression guard for the "packed consumer hangs for 6 hours" bug: an embedded | ||
| // PGlite store that runs a transactional workload (migrations + a write) and | ||
| // then `close()`s must let its process EXIT. PGlite's WASM arms Emscripten | ||
| // `setitimer` timers for Postgres transaction timeouts; those stay ref'd on the | ||
| // Node event loop unless the underlying PGlite instance is actually closed. | ||
| // `createPgliteStorage` owns its pool, so its `close()` must end it. | ||
| // | ||
| // This spawns a one-shot child so the assertion is the real thing we care about | ||
| // — the process terminating on its own — rather than an indirect handle count | ||
| // (vitest's own runtime keeps timers alive, so `getActiveResourcesInfo()` in- | ||
| // process can't distinguish the leak). If the bug returns, the child hangs and | ||
| // `execFileSync`'s timeout kills it, throwing here. | ||
| describe('createPgliteStorage process teardown', () => { | ||
| it('lets a one-shot process exit after close()', () => { | ||
| const dir = mkdtempSync(join(tmpdir(), 'pglite-teardown-')) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the temporary database after the test. Each run leaves this directory, the child script, and the persistent PGlite database in the system temporary directory. Failure and timeout paths also leave them behind. Wrap script creation and child execution in
|
||
| const pgliteSrc = fileURLToPath( | ||
| new URL('../src/pglite.ts', import.meta.url), | ||
| ) | ||
| const script = join(dir, 'exit.mts') | ||
| // Two open→write→close cycles on the same dataDir — the shape a real app | ||
| // (and the packed-consumer smoke's runtime.mjs) exercises. One un-closed | ||
| // instance only *delays* exit; the second leaves a self-re-arming WASM timer | ||
| // that pins the loop forever, which is the true 6-hour wedge. | ||
| const idx = (t: string) => | ||
| `{ published: true, locale: 'en', slug: ${JSON.stringify(t)}, title: ${JSON.stringify(t)}, publishAt: null, unpublishAt: null, deletedAt: null }` | ||
| writeFileSync( | ||
| script, | ||
| `import { createPgliteStorage } from ${JSON.stringify(pgliteSrc)} | ||
| const dataDir = ${JSON.stringify(join(dir, 'data'))} | ||
| for (const id of ['a', 'b']) { | ||
| const s = createPgliteStorage({ dataDir }) | ||
| await s.init() | ||
| await s.putDocument( | ||
| { collection: 'post', id, doc: { title: id, published: true }, updatedAt: new Date().toISOString() }, | ||
| ${idx('t')}, | ||
| ) | ||
| await s.close() | ||
| } | ||
| console.log('exited-clean') | ||
| `, | ||
| ) | ||
| const tsx = fileURLToPath( | ||
| new URL('../../../node_modules/.bin/tsx', import.meta.url), | ||
| ) | ||
| // If close() leaves PGlite's timers armed, the child never exits; the | ||
| // timeout then kills it and execFileSync throws, failing the test. | ||
| const out = execFileSync(tsx, [script], { | ||
| timeout: 30_000, | ||
| encoding: 'utf8', | ||
| stdio: ['ignore', 'pipe', 'pipe'], | ||
| }) | ||
| expect(out).toContain('exited-clean') | ||
| }, 40_000) | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closing during engine creation can leave the PGlite instance open.
createPglitePool().end()closes only thedbvalue that exists when it runs (packages/store-postgres/src/pglite.ts:108-112).open()assignsdbonly afterPGlite.create()resolves (pglite.ts:52-57). If the acquisition timeout expires, orclose()runs, while creation is still pending,end()finds no engine. The pendingopen()then assignsdb, and nothing closes it. The engine and its timers stay ref'd, so the process can still fail to exit afterclose()resolves.Shutdown must not leave a created engine open. Either
end()must await a pendingopen()and then close the resulting instance, oropen()must close an instance that resolves after shutdown has begun. The completed-creation path already closes correctly.