WASI build: setjmp/longjmp error recovery works under standardized wasm EH (wasi-sdk 33 + wazero) — findings and patches from embedding pglite.wasi in Go
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 35/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 静か
- 領域
- backend, build-system, databases
調査の方向性
build/build.sh と参照されている build/patches/0002…0006 から始め、固定された WASI ソースと wazero のセットアップで go test ./conformance/ を実行します。elog.c、pglite-wasm/pgl_sjlj.c、pgl_initdb、interactive_one の wire loop に関わる変更を確認します。完了とは、関連する修正がプロジェクトに適した変更として分離され、再現可能なビルドが通り、conformance のエラーリカバリスイートが引き続きグリーンであることを意味します。
索引モデルが issue の本文から書いたものです。
説明
Summary
We embedded the REL_17_5_WASM-pglite WASI build (branch head 2194acf, built with the in-tree wasm-build.sh + portable-sdk 3.1.74.12.0) in a Go host under wazero, and got full PostgreSQL ERROR recovery working in the WASI flavor: erroring queries come back as normal wire-level errors with correct SQLSTATEs and the same instance keeps serving. Our stress gate — 120 mixed error/success iterations, aborted-transaction semantics (25P02 → ROLLBACK), a 1000-error storm on one instance, deep-recursion limits — passes 200 consecutive runs with real pgx clients.
Along the way we found and fixed several issues that we believe affect any consumer of the WASI build, not just ours. Everything below is maintained as a small patch series against 2194acf (build/patches/0002…0006 in https://github.com/moznion/wasipg, built reproducibly by build/build.sh); we'd be glad to turn any of it into PRs if there's interest.
1. Enabling sjlj (the headline)
The "sjlj exception handler off" state turns out to be three separate layers; with all three flipped, PG's own sigsetjmp recovery works out of the box:
- Toolchain: the portable-sdk's
hotfix/patch.h(force-included after user CFLAGS) stubssigsetjmptoreturn 0andsiglongjmpto a no-op. Since PG 17's no-sigsetjmp fallback inc.his Windows-only, these stubs are the only definition the build ever sees. We rewrite them to realsetjmp/longjmpwhen aWASIPG_SJLJmacro is set (this has to happen in patch.h itself —a later-includecannot win the macro-redefinition race). src/backend/utils/error/elog.c: the#if defined(__wasi__)ERROR →abort()inerrfinish(patch 0002; also makes the non-sjlj abort emit the error report first, so hosts can at least see why).pglite-wasm/pgl_sjlj.c:#if defined(__wasi__)compiles out the whole sigsetjmp block including thePG_exception_stackhookup (the code that prints "sjlj exception handler off"), so every ERROR is promoted to FATAL bypg_re_throw's NULL-stack branch (patch 0005).
Build flags: -mllvm -wasm-enable-sjlj -mllvm -wasm-use-legacy-eh=false plus -lsetjmp at link (patch 0004), compiled with wasi-sdk 33 (LLVM 22). The -wasm-use-legacy-eh=false part matters: LLVM's default legacy EH opcodes are rejected by runtimes that implement the standardized exnref proposal (wazero ≥ 1.12 in our case; the legacy opcodes are on their way out generally). wasi-sdk 25's LLVM 19 cannot emit exnref sjlj at all, hence the toolchain bump.
Measured cost of sjlj on the module: +0.2% size, no measurable boot or per-query latency change.
2. Silent shadow-stack overflow (worth fixing for every consumer)
The wasi-c wrapper hardcodes -z stack-size=131072 after user flags, so builds get a 128 KiB shadow stack that cannot be overridden from build scripts. Combined with the --no-stack-first layout, those 128 KiB sit directly above the data section — PostgreSQL's parser/planner recursion overflows into global data long before PG's max_stack_depth check (2 MB default) can trigger, silently corrupting arbitrary state. We saw intermittent traps at completely unrelated sites (relcache hash, ProcArray, ResourceOwner) until we raised the wrapper's stack size to 8 MiB; after that, deep recursion surfaces as PostgreSQL's clean, recoverable stack-depth/parser-depth ERROR. Recommendation: make the stack size overridable (or default it much higher for the PG build), or use --stack-first so overflow traps instead of corrupting.
3. freopen(stdin) in pgl_initdb breaks under wazero and newer wasi-libc
Two independent problems with the initdb boot-script replay (freopen(IDB_PIPE_BOOT, "r", stdin)):
- wazero's
fd_renumberrefuses stdio targets (it marks fds 0–2 as preopens), so wasi-libc's freopen (open + dup3) fails — and musl's failure path then closes stdin outright. The replay dies withinput in flex scanner failed. (Reported separately to wazero: https://github.com/wazero/wazero/issues/2518) - wasi-libc 33 declares
stdinasFILE *const, so the existingstdin = fdopen(saved_stdin, "r")restore no longer compiles.
Patch 0003 redirects at the fd level instead: close(0); open(...) lands the script on fd 0 via lowest-free allocation, no stream reassignment needed. Works under both wazero and wasmtime.
4. Wire-loop findings in interactive_one (WASI socket-file transport)
Driving the module hard with a real pgx client surfaced several issues in the per-tick wire loop (all addressed in patch 0006):
- The tick prologue is not idempotent, so hosts must not call
interactive_oneas a "poll" when they merely want pending output: the prologue resets MessageContext (freeing memory still referenced by a half-processed extended-protocol batch), invalidates the catalog snapshot, and consumes thesend_ready_for_queryflag. A host-scheduling-dependent number of poll ticks made identical runs diverge into traps, hangs and off-by-one reply pairing. We added a side-effect-freewasipg_flushexport (emit the deferred ReadyForQuery, rename buffered reply bytes onto the reply file, touch nothing else) and drive real ticks only to consume input; something like it would be a great official addition to the ABI. - The idle-tick block that real
PostgresMainuses to send ReadyForQuery only cleared the flag — if the previous tick could not emit RFQ (auth in progress), the deferred RFQ was silently dropped. wire_flushskipped thePGS_OLOCK → PGS_OUTrename when thesockfilesflag was momentarily false while reply bytes were buffered; the next tick'sfopen(OLOCK, "w")then truncated the stranded reply.- Bytes left in the pq receive buffer by a partially-processed batch (error mid-pipeline) were never drained by later ticks — the tail, often the Sync, never executed and the client hung. Empty ticks now route into the wire loop when leftover pq bytes exist.
- The recovery sigsetjmp armed at the
incoming:label, after io_init / snapshot invalidation / startup-auth processing — an ERROR raised in that window threw a longjmp no handler in that invocation had registered: uncaught exception, instance dead. The arming now happens at the top of the tick.
Two host-facing semantics that cost us time and may be worth documenting for binding authors:
- After error recovery,
pq_comm_resetcan leave libpq's send accounting in a state where a later flush re-emits an already-delivered reply; a host that trusts the byte stream blindly gets its request/reply pairing shifted by one. We guard host-side (each exchange is sealed at its ReadyForQuery). - The auth handshake exchange legitimately ends without ReadyForQuery (e.g.
AuthenticationMD5Passwordawaiting the client), so "wait for Z" is not a valid completion rule during startup.
Repro / artifacts
Everything is reproducible from a clean checkout of https://github.com/moznion/wasipg: ./build/build.sh builds the bundle from pinned sources (branch REL_17_5_WASM-pglite @ 2194acf, portable-sdk 3.1.74.12.0, wasi-sdk 33 overlay; every input sha256-pinned), and go test ./conformance/ runs the error-recovery suite with a real pgx client under wazero. Happy to rebase the patch series onto whatever branch is most useful, split it into PRs, or share more traces.
- 主要言語
- TypeScript
- スター
- 16.3k
- フォーク
- 474
- 平均マージ
- 23時間 53分
- マージ済み PR(30日)
- 53
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
electric-sql/pglite のほかの issue
-
Feature request: Nitro 3 support (`unwasm` export condition)再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
electric-sql/pglite#1120 · コメント 16 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
electric-sql/pglite#1200 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 54/100
electric-sql/pglite#1192 ·
メンテナーはふだん 1 日以内に返信
-
[BUG]: syncToFs() resolves immediately, without syncing, when another sync is already queued対応中かも @PrabalParihar が 11 日前に担当しました。 オープンbug
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
electric-sql/pglite#1191 ·
メンテナーはふだん 1 日以内に返信
-
Feature request: live: allow pausing the notify triggers during bulk writes (e.g. pglite-sync applies) and refresh once on resume対応中かも @tdrz が 13 日前に担当しました。 オープンenhancement
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
electric-sql/pglite#1184 · コメント 2 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
electric-sql/pglite の issue をすべて見る
似ている issue
-
area:cli area:security bug priority:medium
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
"code source" and "numerique.gouv" links: "new window" mention duplicated in the accessible nameオープンaccessibility
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
numerique-gouv/lasuite-landingpage#315 ·
メンテナーはふだん 1 日以内に返信
-
needs:triage
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
HyeokjaeChoi/vaultme#585 ·
メンテナーはふだん 1 日以内に返信
-
enhancement priority/P2
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
beettlle/pi-smart-router#200 ·