fix: avoid Bun NAPI teardown crash on gateway exit#245
Merged
Conversation
Bun 1.3.x panics with a C++ exception when NAPI modules (onnxruntime-node loaded by fastembed) are torn down during process.exit(). This affects the standalone lore binary on every normal shutdown. Fix: - Add safeExit() utility that uses libc _exit() via bun:ffi to skip NAPI teardown entirely. Falls back to process.exit() on Node.js. - Use safeExit() for all post-shutdown exit paths in start.ts and run.ts. - Gracefully shut down the embedding worker in shutdown() before exiting. - Add double-signal guards to prevent concurrent shutdown() from rapid Ctrl-C. - Replace inline FFI _exit block in --check-embeddings with safeExit().
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.
Summary
safeExit()utility using libc_exit()viabun:ffito skip NAPI teardown on the Bun standalone binaryProblem
When the
lorestandalone Bun binary shuts down viaprocess.exit(), Bun panics with a C++ exception during NAPI module teardown. The fastembed module loads onnxruntime-node (a NAPI addon with C++ bindings) on the main thread. Whenprocess.exit()fires, Bun's teardown invokes the C++ destructors which throw.The codebase already had an inline FFI
_exit()workaround in--check-embeddings, but it was not applied to normal shutdown paths.Changes
packages/gateway/src/cli/exit.tssafeExit(code)— usesbun:ffito call libc_exit(), falls back toprocess.exit()on Node.jspackages/gateway/src/cli/start.tssafeExit(), add double-signal guard, shut down embedding worker inshutdown()packages/gateway/src/cli/run.tssafeExit()for all exit paths, add double-signal guardpackages/gateway/src/cli/main.ts--check-embeddingswithsafeExit()Verification
bun run typecheck— passes all 4 packagesbun test— 1087 tests pass, 0 failuresbun run build— all packages build successfully