Description
When using opencode-mem plugin with Bun runtime, exiting the TUI via Ctrl+C consistently crashes Bun with:
panic: NAPI FATAL ERROR: Error::New napi_create_error
Root Cause
The plugin registers a SIGINT/SIGTERM handler in dist/index.js that calls process.exit() after cleanup. When exiting opencode TUI:
- Plugin's SIGINT handler fires, calls
memoryClient.close() then process.exit(0)
onnxruntime-node (used via @xenova/transformers for local embeddings) has an InferenceSession loaded in a worker thread
process.exit() triggers process termination — Bun starts tearing down the NAPI environment
- The ONNX Runtime GC finalizer (
InferenceSessionWrap::FinalizeCallback) tries to destroy the session
- During destruction,
Napi::Error::New(napi_env__*) is called, but the napi_env is already partially destroyed
- Bun panics with
NAPI FATAL ERROR: Error::New napi_create_error
Crash Stack (from macOS crash report)
InferenceSessionWrap::FinalizeCallback()
→ InferenceSessionWrap::~InferenceSessionWrap()
→ Napi::ObjectWrap<InferenceSessionWrap>::~ObjectWrap()
→ Napi::Error::New(napi_env__*)
→ Napi::Error::Fatal() → Bun panic
Environment
- Bun: 1.3.14
- opencode-mem: 2.14.3
- opencode: 1.15.10
- macOS 15.5 (Apple Silicon arm64)
- onnxruntime-node: 1.14.0
Suggested Fix
Remove process.exit() from the shutdownHandler — let the host process manage lifecycle:
const shutdownHandler = async () => {
try {
if (webServer) await webServer.stop();
memoryClient.close();
// DO NOT call process.exit()
} catch (error) {
log("Shutdown error", { error: String(error) });
}
};
Related
See also #97, #88 for other NAPI/native addon issues on arm64.
Description
When using
opencode-memplugin with Bun runtime, exiting the TUI via Ctrl+C consistently crashes Bun with:Root Cause
The plugin registers a SIGINT/SIGTERM handler in
dist/index.jsthat callsprocess.exit()after cleanup. When exiting opencode TUI:memoryClient.close()thenprocess.exit(0)onnxruntime-node(used via@xenova/transformersfor local embeddings) has anInferenceSessionloaded in a worker threadprocess.exit()triggers process termination — Bun starts tearing down the NAPI environmentInferenceSessionWrap::FinalizeCallback) tries to destroy the sessionNapi::Error::New(napi_env__*)is called, but thenapi_envis already partially destroyedNAPI FATAL ERROR: Error::New napi_create_errorCrash Stack (from macOS crash report)
Environment
Suggested Fix
Remove
process.exit()from theshutdownHandler— let the host process manage lifecycle:Related
See also #97, #88 for other NAPI/native addon issues on arm64.