Skip to content

fix: serialize close with in-flight queries - #1089

Open
seanwessmith wants to merge 3 commits into
electric-sql:mainfrom
seanwessmith:agent/serialize-close-with-queries
Open

fix: serialize close with in-flight queries#1089
seanwessmith wants to merge 3 commits into
electric-sql:mainfrom
seanwessmith:agent/serialize-close-with-queries

Conversation

@seanwessmith

@seanwessmith seanwessmith commented Aug 18, 2026

Copy link
Copy Markdown

What changed

  • share one close promise across repeated calls
  • wait for previously-started transactions and queries before deactivating the WASM backend
  • reject operations started after close begins, including while initialization is still completing
  • keep lock coordination separate from the existing shutdown routine
  • add bounded regression coverage for query, transaction, initialization, and repeated-close behavior
  • add the required patch changeset

Why

A query can pass _checkReady() and still be waiting between protocol phases when close() deactivates the backend. The query then enters execProtocolRawSync() against an inactive backend and can block the event loop permanently.

The close path now preserves call order: work invoked before close() drains under the existing transaction/query mutex order, while work invoked afterward sees PGlite is closing.

Fixes #1084.

Related work

#1063 addresses a separate close race with detached filesystem syncs. Both changes affect shutdown ordering and may require conflict resolution if #1063 lands first; this PR specifically serializes close() with in-flight query and transaction work.

Validation

Using the current-main Node 24 WASM artifact from #1087, from packages/pglite:

../../node_modules/.bin/tsup
../../node_modules/.bin/tsc --noEmit
../../node_modules/.bin/eslint ./src ./tests --report-unused-disable-directives --max-warnings 0
../../node_modules/.bin/prettier --check ./src ./tests
./node_modules/.bin/vitest run tests/targets/runtimes/node-close.test.js
./node_modules/.bin/vitest run tests/targets/runtimes/node-*.test.js
  • focused close regressions: 3 passed
  • Node runtime suite: 13 passed

@seanwessmith
seanwessmith marked this pull request as ready for review August 18, 2026 17:39
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.

close() racing an in-flight statement wedges PGlite permanently and blocks the event loop

1 participant