Skip to content

fix(wren-core-wasm): drive query() through a tokio runtime (UNION ALL trap)#2291

Merged
PaulChen79 merged 3 commits into
mainfrom
fix/wasm-union-all-v2
May 15, 2026
Merged

fix(wren-core-wasm): drive query() through a tokio runtime (UNION ALL trap)#2291
PaulChen79 merged 3 commits into
mainfrom
fix/wasm-union-all-v2

Conversation

@goldmedal
Copy link
Copy Markdown
Collaborator

@goldmedal goldmedal commented May 15, 2026

Summary

@wrenai/wren-core-wasm@0.4.0 (and 0.3.0 before it) traps with
RuntimeError: unreachable on any SQL whose physical plan contains
CoalescePartitionsExec over more than one partition — UNION ALL,
UNION, INTERSECT, EXCEPT, and some parallel hash joins. The
trivial form (no MDL, no parquet, no tables) reproduces:

await engine.query(\`SELECT a FROM (SELECT 1 AS a UNION ALL SELECT 2) t\`);
// → RuntimeError: unreachable

Reported in BUG_REPORT_wren_core_wasm_union_all.md.

Root cause

CoalescePartitionsExec::execute (DataFusion 53) spawns one task per input
partition via JoinSet::spawntokio::task::spawn. That spawn panics
with

there is no reactor running, must be called from the context of a Tokio 1.x runtime

because `wasm-bindgen-futures` drives Rust futures directly off JS
microtasks and never enters a tokio runtime. Single-partition queries hit
the `1 =>` short-circuit in `CoalescePartitionsExec` and never spawn,
which is why every `registerJson` + raw `query` example, every cube
query, and every dashboard query worked — and the bug stayed hidden until
the first `UNION ALL` in the wild.

The bug is wasm32-only: the same SQL + same DataFusion 53.0.0 + same
crate feature set succeeds when compiled natively (verified). It's not a
0.4.0 regression — 0.3.0 traps identically.

Fix

  • `WrenEngine` now owns a `current_thread` `tokio::runtime::Runtime`.
  • `query()` wraps its body in `runtime.block_on(async { … })`; `cube_query()` delegates to `query()` so it inherits the fix.
  • `WrenEngine::new` installs `console_error_panic_hook::set_once()`. Future panics surface in the JS console with a Rust message instead of the opaque `RuntimeError: unreachable` that hid this bug for two releases.

The public JS surface is unchanged — both methods are still `async` and
still return a Promise. The block_on drives the inner future synchronously
on the JS thread (which is what wasm-single-thread already does anyway).

Coverage

  • 6 Node SDK tests under `describe("set operators", …)`: `UNION ALL` subquery + top-level, `UNION` dedup, `INTERSECT`, `EXCEPT`, and `UNION ALL` across two registered tables (sdk/tests/index.test.mjs — 34 total tests now pass).
  • 1 Rust `wasm_bindgen_test` (`test_union_all_does_not_trap`).
  • 1 native `wren-core` test (`test_union_all_local_runtime`) confirms the wren-core LocalRuntime path itself is unaffected — the bug really is wasm32-only.

Verification

$ node verify-union.mjs   # bug-report minimal repro + sibling set ops
✓ UNION ALL in subquery (bug repro): 2 row(s)
✓ top-level UNION ALL: 2 row(s)
✓ UNION (dedup): 2 row(s)
✓ INTERSECT: 1 row(s)
✓ EXCEPT: 1 row(s)
✓ simple aggregate (regression): 1 row(s)
6 passed, 0 failed

$ just test    # full SDK suite
ℹ pass 34  ℹ fail 0

Release

This should ship as a `@wrenai/wren-core-wasm` patch (`0.4.1`). The fix
is purely additive on the Rust side (new field, new dep `console_error_panic_hook`) — the JS API is unchanged.


🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Fixed set operators (UNION ALL, UNION, INTERSECT, EXCEPT) that were causing failures in WASM environments
    • Improved error visibility with enhanced console error reporting
  • Tests

    • Added comprehensive test coverage for set operators and regression tests to prevent future issues

Review Change Stack

`@wrenai/wren-core-wasm` 0.3.0 and 0.4.0 trap with
`RuntimeError: unreachable` on any SQL whose physical plan contains
`CoalescePartitionsExec` over more than one partition — `UNION ALL`,
`UNION`, `INTERSECT`, `EXCEPT`, and some parallel hash joins. The
trivial `SELECT a FROM (SELECT 1 AS a UNION ALL SELECT 2) t` reproduces
without any MDL or registered tables.

Root cause: `CoalescePartitionsExec::execute` (DataFusion 53)
spawns one task per input partition via `JoinSet::spawn` →
`tokio::task::spawn`. That spawn panics with
`there is no reactor running, must be called from the context of a
Tokio 1.x runtime` because `wasm-bindgen-futures` drives Rust futures
straight off JS microtasks without ever entering a tokio runtime.
Single-partition queries hit `CoalescePartitionsExec`'s short-circuit
branch and never spawn, which is why every non-set-op dashboard query
worked and the bug stayed hidden until now.

Fix: own a `current_thread` `tokio::runtime::Runtime` on `WrenEngine`
and run the body of `query()` (and therefore `cube_query()`, which
delegates) inside `runtime.block_on(...)`. block_on drives the inner
future to completion synchronously, so DataFusion's spawned tasks now
have a live scheduler to make progress on.

Also adopt `console_error_panic_hook::set_once()` in `WrenEngine::new`
so any future panic surfaces in the JS console with a Rust message
instead of the opaque `unreachable` trap that hid this bug for two
releases.

Coverage:
- 6 new Node SDK tests under "set operators": UNION ALL (subquery +
  top-level), UNION, INTERSECT, EXCEPT, and UNION ALL across two
  registered tables.
- 1 new Rust `wasm_bindgen_test` (`test_union_all_does_not_trap`).
- 1 native wren-core test (`test_union_all_local_runtime`) documents
  that the wren-core LocalRuntime path itself is fine — the bug is
  wasm32-only.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added dependencies Pull requests that update a dependency file rust Pull requests that update rust code core wasm labels May 15, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: c4df2311-4c58-4385-b3c1-9b046dca0243

📥 Commits

Reviewing files that changed from the base of the PR and between 6ebed77 and 3247f1b.

⛔ Files ignored due to path filters (1)
  • core/wren-core-wasm/Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (4)
  • core/wren-core-wasm/Cargo.toml
  • core/wren-core-wasm/sdk/tests/index.test.mjs
  • core/wren-core-wasm/src/lib.rs
  • core/wren-core/core/src/mdl/mod.rs

Walkthrough

This PR fixes WASM runtime traps when executing set-operator queries by introducing an owned tokio current-thread runtime to WrenEngine. The constructor now installs a panic hook and builds the runtime; query() executes within block_on() to provide a live reactor for DataFusion's internal task spawning. Regression tests validate the fix across WASM and core platforms.

Changes

Set Operator Runtime Fix

Layer / File(s) Summary
WASM Runtime Field and Panic Hook Setup
core/wren-core-wasm/Cargo.toml, core/wren-core-wasm/src/lib.rs
WrenEngine now owns a tokio::runtime::Runtime field. Constructor installs console_error_panic_hook for JS console panic visibility and builds a current-thread runtime via tokio::runtime::Builder. The console_error_panic_hook dependency is added to Cargo.toml.
Query Execution Within Runtime
core/wren-core-wasm/src/lib.rs
WrenEngine::query() wraps all async DataFusion operations and JSON serialization inside self.runtime.block_on(...) to ensure spawned tasks execute within the engine's tokio scheduler. cube_query() explicitly documents that it delegates to query() for runtime entry.
Regression Test Coverage
core/wren-core-wasm/src/lib.rs, core/wren-core/core/src/mdl/mod.rs, core/wren-core-wasm/sdk/tests/index.test.mjs
WASM regression test validates UNION ALL does not trap. Core MDL regression test reproduces the trap scenario under Mode::LocalRuntime. SDK test suite covers UNION ALL (subquery and top-level), UNION deduplication, INTERSECT, and EXCEPT across registered tables.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • Canner/WrenAI#2278: Both PRs modify core/wren-core-wasm/src/lib.rs's WrenEngine and its execution path; #2278 adds cubeQuery()/listCubes() delegating into query(), while this PR changes query() runtime wiring to prevent WASM runtime traps, directly affecting those cube APIs.

Suggested labels

rust, core, wasm


🐰 A tokio thread joins the dance,
Set operators get their chance,
No more traps in WASM land,
Block_on's magic, isn't grand?
The compiler nods, "It's planned!" ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main fix: addressing a UNION ALL trap by driving query() through a tokio runtime in wren-core-wasm.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/wasm-union-all-v2

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@goldmedal goldmedal requested a review from PaulChen79 May 15, 2026 08:10
@PaulChen79 PaulChen79 merged commit f0748f5 into main May 15, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core dependencies Pull requests that update a dependency file rust Pull requests that update rust code wasm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants