Skip to content

fix(pglite): make close() wait for queries issued before it - #1170

Merged
tdrz merged 1 commit into
electric-sql:mainfrom
sarankumar1325:fix/close-waits-for-queued-queries
Oct 28, 2026
Merged

tdrz merged 1 commit into
electric-sql:mainfrom
sarankumar1325:fix/close-waits-for-queued-queries

Conversation

@sarankumar1325

Copy link
Copy Markdown
Contributor

Fixes #1168

Problem

close() only waited on the query mutex. But the public query(), exec() and transaction() take the transaction mutex first and the query mutex second. So a call issued just before close() can still be waiting on the transaction mutex when close() grabs the query mutex, sets #closing and tears down the module. Once that call reaches the query mutex, it runs against the torn-down module and fails with:

TypeError: Cannot read properties of undefined (reading '_PostgresSendReadyForQueryIfNecessary')

Fix

close() now takes the transaction mutex around the existing query-mutex section. Anything queued through query(), exec() or transaction() before close() finishes first, and only then does teardown begin. Nothing else changes: init still isn't blocked, and the _checkReady() re-check for concurrent close() calls is still in place.

Tests

  • Added close waits for queries issued before it to tests/basic.test.ts (runs for both ESM and CJS). It starts a query(), an exec() and a transaction() without awaiting them, calls close(), and then checks that all three resolve with their results. On main it fails with the error above. With this change it passes.
  • vitest over tests/** (the test:basic set): 321 passed, 1 skipped. tests/targets/runtimes/node-*: 12 passed.
  • pnpm stylecheck and pnpm typecheck are clean.
  • Added a patch changeset for @electric-sql/pglite.

I tested locally against the wasm build artifacts from the latest main CI run.

I used an AI coding assistant to write this change. I reviewed it and ran the tests above.

close() only took the query mutex, but query(), exec() and transaction()
take the transaction mutex first. A call issued just before close() could
therefore reach the query mutex after teardown and fail with
"Cannot read properties of undefined". Take the transaction mutex too.

Fixes electric-sql#1168
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-28T01:36:41.992360Z
We couldn’t complete this review. Comment @codex review to try again.
a950ddf PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@tdrz
tdrz self-requested a review October 28, 2026 10:08

@tdrz tdrz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@tdrz
tdrz merged commit c7dc39e into electric-sql:main Oct 28, 2026
14 of 17 checks passed
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.

[BUG]: close() still lets a query sent just before it reach the closed engine (follow-up to #1128)

2 participants