Skip to content

fix: preserve host process.exitCode when closing PGlite - #1059

Merged
tdrz merged 1 commit into
electric-sql:mainfrom
zakcutner:fix/preserve-exit-code-on-close
Jul 28, 2026
Merged

tdrz merged 1 commit into
electric-sql:mainfrom
zakcutner:fix/preserve-exit-code-on-close

Conversation

@zakcutner

Copy link
Copy Markdown
Contributor

Closing a PGlite instance resets the host process's process.exitCode to 0. This wipes out whatever the host had already set.

The cause is close() calling _emscripten_force_exit(0). Emscripten implements exit as quit_ = (e, r) => { throw process.exitCode = e, r }, so it writes process.exitCode = 0 on the host Node process. The #init() and execProtocolRaw() paths already save and restore process.exitCode around their Emscripten calls, but close() was missing the same guard.

This adds that guard. It reuses the existing pglUtils.pgliteProc.exitCode save and restore pattern in a finally, like execProtocolRaw(). It also adds a test next to the existing restores process.exitCode one, plus a changeset.

close() calls _emscripten_force_exit(0), whose Emscripten runtime sets
process.exitCode = 0 and clobbers any exit code the host process had
already set. Save and restore process.exitCode around the call, matching
the existing guards in #init() and execProtocolRaw().

@tdrz tdrz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for this!

@tdrz
tdrz merged commit 20839a7 into electric-sql:main Jul 28, 2026
14 checks passed
@zakcutner
zakcutner deleted the fix/preserve-exit-code-on-close branch July 28, 2026 19:52
AntonOfTheWoods added a commit to pgxsinkit/pglite that referenced this pull request Aug 18, 2026
Moved pgx-publish back onto upstream/main (0.5.5). initdb-fs-leak (electric-sql#1060)
and formatQuery positional-params (electric-sql#1056) merged upstream, so both are
dropped from the fork; the remaining stack carries only changes still
absent upstream: transaction-end sync (+ already-closed completion), the
IDBFS durability hardening and relaxed-concurrency restore, the
non-exclusive relaxed-sync latch (scoped), the opfs-ahp instrumentation,
and the close() relaxed-sync drain.

Conflicts re-applied onto the new base:
- pglite.ts close(): kept the IDBFS-hardening deferred-throw structure and
  folded upstream electric-sql#1059's process.exitCode save/restore into force_exit.
- base.ts transaction(): our flag-repositioning + rollback/already-closed
  sync guards now coexist with upstream electric-sql#1062's closed=true-before-ROLLBACK
  and checkClosed() gates.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
AntonOfTheWoods added a commit to pgxsinkit/pglite that referenced this pull request Aug 18, 2026
PGlite saves and restores process.exitCode around engine calls that may run
proc_exit(XX). On a clean host the saved value is undefined, and under bun
`process.exitCode = undefined` is a no-op that leaves the current value in
place — so the restore silently failed and the engine's proc_exit(99) boot
sentinel survived, force-exiting an otherwise successful host process with
code 99.

close() previously masked this: _emscripten_force_exit(0) set an explicit 0
on the way out. Preserving the host exit code across close() (electric-sql#1059) removed
that incidental reset and exposed the bug on every lane — a green bun test
run now exits 99.

Route all three restore sites through a helper that writes 0 when the saved
value was undefined; that is equivalent for exit status on every runtime.

Co-Authored-By: Claude Opus 5 <[email protected]>
AntonOfTheWoods added a commit to pgxsinkit/pglite that referenced this pull request Aug 18, 2026
0.5.5-pgx.1 regressed every bun host lane: upstream electric-sql#1059's process.exitCode
save/restore across close() removed the incidental _emscripten_force_exit(0)
reset that had been masking a failed restore, so the engine's proc_exit(99)
boot sentinel reached process exit. Green pgxsinkit unit shards exited 99.

Co-Authored-By: Claude Opus 5 <[email protected]>
AntonOfTheWoods added a commit to pgxsinkit/pglite that referenced this pull request Sep 12, 2026
Moved pgx-publish back onto upstream/main (0.5.5). initdb-fs-leak (electric-sql#1060)
and formatQuery positional-params (electric-sql#1056) merged upstream, so both are
dropped from the fork; the remaining stack carries only changes still
absent upstream: transaction-end sync (+ already-closed completion), the
IDBFS durability hardening and relaxed-concurrency restore, the
non-exclusive relaxed-sync latch (scoped), the opfs-ahp instrumentation,
and the close() relaxed-sync drain.

Conflicts re-applied onto the new base:
- pglite.ts close(): kept the IDBFS-hardening deferred-throw structure and
  folded upstream electric-sql#1059's process.exitCode save/restore into force_exit.
- base.ts transaction(): our flag-repositioning + rollback/already-closed
  sync guards now coexist with upstream electric-sql#1062's closed=true-before-ROLLBACK
  and checkClosed() gates.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
AntonOfTheWoods added a commit to pgxsinkit/pglite that referenced this pull request Sep 12, 2026
0.5.5-pgx.1 regressed every bun host lane: upstream electric-sql#1059's process.exitCode
save/restore across close() removed the incidental _emscripten_force_exit(0)
reset that had been masking a failed restore, so the engine's proc_exit(99)
boot sentinel reached process exit. Green pgxsinkit unit shards exited 99.

Co-Authored-By: Claude Opus 5 <[email protected]>
AntonOfTheWoods added a commit to pgxsinkit/pglite that referenced this pull request Sep 12, 2026
Moved pgx-publish onto upstream/main (0.5.8, ae182ff). The remaining stack
carries only changes still absent upstream: transaction-end sync (plus the
already-closed-transaction completion), the IDBFS durability hardening and
relaxed-concurrency restore, the non-exclusive relaxed-sync latch (scoped),
the opfs-ahp instrumentation, the close() relaxed-sync drain, the BasePGlite
export and the live-query refresh/diff-key fixes.

Dropped: the explicit-zero exit-code restore (0.5.5-pgx.2, and its
changeset). Upstream electric-sql#1087 and electric-sql#1100 removed the process.exitCode
save/restore sites entirely — the engine now reports exit status through
_pgl_setPGliteExitStatus — so the helper had nothing left to restore. Its
version-bump commit is kept as history.

Conflicts re-applied onto the new base:
- pglite.ts close(): kept the hardened structure (transaction/query mutex
  nesting, pending-sync drain, deferred final-sync and close-hook failures,
  closeFs in a finally, no serialize.end()) and replaced the folded electric-sql#1059
  exitCode save/restore with upstream 0.5.8's exit tail — force_exit(0) with
  `this.mod = undefined` in the finally.
- pglite.ts #init(): upstream removed the prevExitCode capture and its
  restore that bracketed the method, so the fork's #cleanupFailedInit,
  #initialSyncFs and #syncToFsStrict now follow a bare closing brace.
- pglite.ts execProtocolRawSync(): upstream's body — the
  _pgl_setPGliteExitStatus(-2) longjmp check and a finally with no exit-code
  restore — plus the fork's #checkSynchronousFsSyncState() prologue.
- packages/pglite/package.json: upstream's rewritten test:bun script kept
  alongside the fork's package name, repository url and publish:pgx:*
  scripts.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_019muxX7JzHorerHQedWXRoz
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