Skip to content

Recover synchronous PG 18 parser calls after a WASM failure - #206

Closed
pyramation wants to merge 5 commits into
mainfrom
feat/wasm-sync-recovery
Closed

pyramation wants to merge 5 commits into
mainfrom
feat/wasm-sync-recovery

Conversation

@pyramation

@pyramation pyramation commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Discard a failed PG 18 WASM instance and rebuild it from the cached compiled module before the next call, including synchronous calls. Reuse Emscripten’s asset loader and preserve loadModule({ wasm }).
  • Cap large-heap replacements in one synchronous turn. Async recovery honors large failures even when already queued, and retained Emscripten exit errors no longer pin the failed heap. Routine SQL errors keep the current instance.
  • Keep the manual WASM workflow on the same pinned Emscripten SDK as CI and releases.

PG 18 successor to #189. PG 13–17 recovery and SqlError typing are out of scope.

Tests

  • All 150 PG 18 tests pass locally, including immediate recovery across the full API, failed replacement retries, same-turn memory limits, queued async recovery, and heap reclamation while the caller retains a real OOM error.
  • ESM smoke test: a fresh synchronous call succeeds immediately after a memory failure.

libpg-query and parser opened this PR for Dan
Implemented in session Recover sync parser calls after a failed WASM instance
Review with Devin · Open in Devin Desktop


Summary by CodeRabbit

  • Bug Fixes
    • Synchronous operations can now initialize or recover the WebAssembly engine automatically after a failed call, rather than requiring separate initialization.
    • Improved handling of memory exhaustion: oversized operations report a clear error, and synchronous retries may need to wait briefly before trying again.
    • Asynchronous operations can continue after memory-related failures, and stack-depth errors no longer leave the engine unusable.
  • Tests
    • Updated coverage for recovery, initialization failures, and memory-limit behavior.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@luxor-code-review

luxor-code-review Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Review complete. No issues found.


This change reworks how the pg-query WASM binding handles runtime failures: instead of invalidating the module on OOM/crash, the wrapper now builds a fresh synchronous instance from the compiled WASM and swaps it in, with failure counters gating repeated sync replacement.

Files Change
templates/full/index.ts, versions/18/src/index.ts New sync-instantiation recovery path, failure tracking, and invalidateModule handling for OOM and init errors.
templates/full/pre.js, versions/18/src/pre.js instantiateAsync monkeypatch and compiled-WASM sync bootstrap logic for the full variant.
versions/18/test/load-module.test.js, versions/18/test/wasm-recovery.test.js Rewritten loadIsolated harness and updated assertions covering the new sync replacement behavior.
scripts/copy-templates.js Template-sync script updated to handle full-variant concatenation and header insertion.

No verified findings met the review threshold. Files skipped: none — all 7 changed files were reviewed.

Reviewed commit: 599bcd2

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The build workflow now uses Emscripten SDK 6.0.3. The full-API templates and PG 18 wrapper retain compiled WASM modules and use them to create replacement instances after failures. Tests cover synchronous recovery and memory-limit behavior.

Changes: Emscripten build workflow

Layer / File(s) Summary
Update the Emscripten SDK version
.github/workflows/build-wasm-no-docker.yaml
The workflow clones, installs, and activates Emscripten SDK version 6.0.3 instead of 3.1.59.

WebAssembly instance recovery

Layer / File(s) Summary
Wire compiled-module initialization
scripts/copy-templates.js, templates/full/pre.js, versions/18/src/pre.js
The template copier appends the full-API preamble. The preambles retain modules from asynchronous instantiation or initialize synchronously from a retained module.
Retain modules and recover instances
templates/full/index.ts, versions/18/src/index.ts
The wrappers retain compiled modules, invalidate failed instances, and create replacements. Synchronous entry points use shared loading checks, while asynchronous loading waits for an available instance or a scheduled yield.
Validate recovery and memory limits
versions/18/test/load-module.test.js, versions/18/test/wasm-recovery.test.js
Tests cover oversized-input failures, yield-before-retry behavior, synchronous replacement, load failures, wrapped causes, and PG 18 deep-nesting errors.

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant SyncAPI
  participant WasmWrapper
  participant CompiledModule
  SyncAPI->>WasmWrapper: Call WASM
  WasmWrapper-->>SyncAPI: Return error and invalidate instance
  SyncAPI->>WasmWrapper: Make next synchronous call
  WasmWrapper->>CompiledModule: Create replacement instance
  CompiledModule-->>WasmWrapper: Return instance
  WasmWrapper-->>SyncAPI: Return parse result
Loading

Possibly related PRs

Suggested reviewers: luxor-code-review

Merge Risk: Minimal · up to 77f37

This change makes PG 18 synchronous parser calls recover immediately after a WASM failure by rebuilding the instance from the cached compiled module. No actionable merge-blocking risk remains after the review.

Security architecture risk: Low · up to 77f37

Immediate recovery prevents later calls from reusing a failed parser instance. It also lets repeated synchronous failures trigger repeated allocations before memory can be reclaimed. The risk is limited to applications that use the synchronous parser and continue after failures.

Retained concerns

  • Low · reliability · inferred: At the merge base, synchronous calls did not construct new instances inside a retry loop. At the reviewed head, repeated fatal calls can each cause the next synchronous call to allocate a replacement, with no library retry limit for discarded heaps at or below 512 MiB. For larger heaps, a microtask checkpoint can reopen synchronous recovery before the task boundary used by async recovery. In a caller that continues processing failures, overlapping allocations could pressure the embedding process. Actual memory peaks and externally reachable retry behavior were not established.
Security review details

Security Blast Radius

  • inferred — SQL text already reaches WASM through the parser API. The changed failure path affects the instance shared by callers of the same loaded wrapper and can create memory pressure in the embedding process. Cross-tenant or remote exposure depends on how applications share the parser, handle failed calls, and admit untrusted input; those deployment conditions were not established.

Trust Boundaries and Controls

  • observed — SQL input goes to the existing WASM parser call. Executable module selection remains separate: it comes from the default asset loader or the existing loadModule option, and recovery uses the compiled module already retained by the wrapper.

Resilience and Maintainability Implications

  • observed — Fatal-call handling restores the process exit code, drops the failed instance, and copies the ExitStatus payload instead of retaining the original loader error. This removes a known reference from the returned error to the discarded heap.

Hardening Proposals

  • proposed — Measure peak process memory during repeated synchronous recovery, including retries separated only by microtask checkpoints. If the peak exceeds the supported host budget, consider a cumulative replacement budget that resets at a task boundary while preserving the first immediate retry.
Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Recover synchronous PG 18 parser calls after a WASM failure" matches the main change. The PR discards a failed WASM instance and rebuilds it from the cached compiled module so synchronous P…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "Recover synchronous PG 18 parser calls after a WASM failure" matches the main change. The PR discards a failed WASM instance and rebuilds it from the cached compiled module so synchronous PG 18 parser calls recover. The objectives and file summaries (versions/18/src/index.ts, pre.js, and the wasm-recovery tests) all center on this behavior. The title is short, specific, and states the primary change without file lists or vague terms.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between b535594 and 77f3751.

Files selected for processing: 3

  • templates/full/index.ts
  • versions/18/src/index.ts
  • versions/18/test/wasm-recovery.test.js

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

@devin-ai-integration devin-ai-integration Bot 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.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

coderabbitai[bot]

This comment was marked as resolved.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Superseded by #207, which combines the PG18 typed SQL errors and WASM recovery fixes.

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.

1 participant