Skip to content

fix: inject WASM ONNX runtime for Bun to prevent NAPI fatal crash#124

Open
Suknna wants to merge 1 commit into
cortexkit:masterfrom
Suknna:fix/bun-napi-ort-crash
Open

fix: inject WASM ONNX runtime for Bun to prevent NAPI fatal crash#124
Suknna wants to merge 1 commit into
cortexkit:masterfrom
Suknna:fix/bun-napi-ort-crash

Conversation

@Suknna
Copy link
Copy Markdown

@Suknna Suknna commented Jun 1, 2026

Problem

opencode crashes with NAPI FATAL ERROR: Error::New napi_create_error when @cortexkit/opencode-magic-context is enabled on macOS arm64 (issue #95). The crash is triggered by onnxruntime-node (native N-API addon) hitting an incomplete N-API code path in Bun.

Root cause

@huggingface/transformers@4.x statically imports onnxruntime-node. Under Bun, the native N-API binding calls napi_create_error which triggers Bun's fatal panic, crashing the entire host process. The crash is intermittent (happens "after running for a while") and depends on specific N-API error paths.

Fix

Extend the existing WASM ONNX injection (injectWasmOrtForElectron, added for issue #78) to also activate under Bun. Both runtimes now get onnxruntime-web (WASM backend) instead of the native onnxruntime-node, while plain Node continues using the faster native backend.

Detection: process.versions.bun (Bun) || process.versions.electron (Electron) → inject WASM. Neither set (plain Node) → no-op.

Verification

  • onnxruntime-web loads and runs inference under Bun
  • Symbol.for("onnxruntime") injection makes transformers.js use WASM backend
  • ✅ Concurrent model loading works (5 parallel sessions)
  • ✅ TypeScript typecheck passes
  • ✅ Lint passes
  • ✅ Build passes

Trade-off

WASM backend is slower than native CPU, but it's the only reliable path under Bun. The native onnxruntime-node cannot safely load due to Bun's N-API limitations.

Closes #95


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag @codesmith with what you need. Autofix is disabled.


Summary by cubic

Switch Bun and Electron to onnxruntime-web (WASM) for embeddings to avoid onnxruntime-node N-API crashes. Plain Node keeps the native backend; closes #95.

  • Bug Fixes
    • Injects WASM via Symbol.for("onnxruntime") when process.versions.bun or process.versions.electron is set; no-op on plain Node.
    • Prevents Bun panic (NAPI FATAL ERROR: Error::New napi_create_error) and Electron native load issues; falls back to native if injection fails.

Written for commit 2f58845. Summary will update on new commits.

Review in cubic

Bun's N-API compatibility layer panics when onnxruntime-node calls
napi_create_error, crashing the host opencode process (issue cortexkit#95).

Extend the existing Electron WASM injection (issue cortexkit#78) to also
activate under Bun. Both runtimes now get onnxruntime-web (WASM)
instead of the native onnxruntime-node, while plain Node continues
using the faster native backend.

Refs: cortexkit#95
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

No issues found across 1 file

Re-trigger cubic

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.

Bun/opencode crashes with NAPI fatal error when opencode-magic-context is enabled on macOS arm64

1 participant