fix: inject WASM ONNX runtime for Bun to prevent NAPI fatal crash#124
Open
Suknna wants to merge 1 commit into
Open
fix: inject WASM ONNX runtime for Bun to prevent NAPI fatal crash#124Suknna wants to merge 1 commit into
Suknna wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
opencode crashes with
NAPI FATAL ERROR: Error::New napi_create_errorwhen@cortexkit/opencode-magic-contextis enabled on macOS arm64 (issue #95). The crash is triggered byonnxruntime-node(native N-API addon) hitting an incomplete N-API code path in Bun.Root cause
@huggingface/transformers@4.xstatically importsonnxruntime-node. Under Bun, the native N-API binding callsnapi_create_errorwhich 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 getonnxruntime-web(WASM backend) instead of the nativeonnxruntime-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-webloads and runs inference under BunSymbol.for("onnxruntime")injection makes transformers.js use WASM backendTrade-off
WASM backend is slower than native CPU, but it's the only reliable path under Bun. The native
onnxruntime-nodecannot safely load due to Bun's N-API limitations.Closes #95
Need help on this PR? Tag
@codesmithwith what you need. Autofix is disabled.Summary by cubic
Switch Bun and Electron to
onnxruntime-web(WASM) for embeddings to avoidonnxruntime-nodeN-API crashes. Plain Node keeps the native backend; closes #95.Symbol.for("onnxruntime")whenprocess.versions.bunorprocess.versions.electronis set; no-op on plain Node.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.