Skip to content

fix(bun): use native bun:sqlite during startup to avoid N-API crash - #11468

Merged
diegosouzapw merged 2 commits into
cheaper-inference:release/v3.8.51from
TheDemonTuan:fix/bun-sqlite-startup
Aug 25, 2026
Merged

diegosouzapw merged 2 commits into
cheaper-inference:release/v3.8.51from
TheDemonTuan:fix/bun-sqlite-startup

Conversation

@TheDemonTuan

Copy link
Copy Markdown
Contributor

Summary

Avoids loading the better-sqlite3 native Node-API .node binary during Bun startup in bootstrap-env.mjs, sync-env.mjs, and sqliteRuntime.mjs. When running under Bun (process.versions.bun), these pre-server bootstrap paths now use the native bun:sqlite built-in driver directly.

Root Cause & Motivation

When running under Bun on Linux ARM64 (e.g. bun dev/run-standalone.mjs), hasEncryptedCredentials in bootstrap-env.mjs ran before OmniRoute's main database driver initialized. Calling require("better-sqlite3") triggered a native Node-API crash:

Panic: NAPI FATAL ERROR: Error::New napi_get_last_error_info
Bun has crashed
Aborted (core dumped)
Exit code 134

Because native aborts cannot be trapped by JavaScript try/catch, this crashed the process before server boot.

OmniRoute already supports and prefers bun:sqlite when running under Bun (driverFactory.ts). This fix aligns the bootstrap and environment utilities with that existing design.

Key Changes

  1. scripts/build/bootstrap-env.mjs: When process.versions.bun is detected, use require("bun:sqlite").Database with synchronous .query(...).get() instead of requiring better-sqlite3.
  2. scripts/dev/sync-env.mjs: Same guard added to hasEncryptedCredentials.
  3. bin/cli/runtime/sqliteRuntime.mjs: Prioritizes bun-sqlite when process.versions.bun is true.
  4. tests/unit/db-adapters/bunSqliteAdapter.test.ts: Added regression test verifying loadSqliteRuntime prioritizes bun:sqlite under Bun without loading better-sqlite3.
  5. Node.js behavior: 100% unchanged (better-sqlite3 is still used under Node.js).

Validation

  • Tested with bun test tests/unit/db-adapters/bunSqliteAdapter.test.ts: 3 pass, 0 fail.

@diegosouzapw
diegosouzapw merged commit 9ad90fe into cheaper-inference:release/v3.8.51 Aug 25, 2026
7 of 16 checks passed
diegosouzapw pushed a commit that referenced this pull request Aug 25, 2026
…11470)

Validated in a combined 4-PR batch worktree off release/v3.8.51 tip (Bun-native SQLite infrastructure cluster; complements #11468 already merged).
- typecheck:core, file-size, changelog-integrity, complexity, cognitive-complexity — all OK (Docker build itself not executed in this environment; reviewed the Dockerfile.bun diff for correctness)
- Full-repo lint: 228 pre-existing dashboard react-hooks/* findings, unrelated to this diff

Thanks for the two concrete failure modes fixed (missing postinstall.mjs during layer-cached bun install, ARM64 NAPI crash avoided by using the native bun:sqlite smoke check instead of rebuilding better-sqlite3) plus the non-root USER bun hardening.
diegosouzapw pushed a commit that referenced this pull request Aug 25, 2026
Validated in a combined 4-PR batch worktree off release/v3.8.51 tip (complements #11468 and #11470, both merged first). Conflicted against #11470's Dockerfile.bun hunk in the shared worktree — resolved additively (both the ownership hardening and the addon-stripping RUN block coexist, ordered so the strip runs as root before USER bun) and pushed the same resolution to this branch.
- Focused test: bun-runtime-native-addon.test.mjs — 1/1 pass, part of batch's 5/5 node:test run
- typecheck:core, file-size, changelog-integrity, complexity, cognitive-complexity — all OK
- Full-repo lint: 228 pre-existing dashboard react-hooks/* findings, unrelated to this diff

Thanks for the isolated-probe root-causing (better-sqlite3 specifically, ruling out keytar/onnxruntime-node/sqlite-vec/tls-client-node/wreq-js/sharp) plus the production ARM64 evidence.
diegosouzapw pushed a commit that referenced this pull request Aug 26, 2026
…11428)

Validated in a combined dependabot worktree off release/v3.8.51 tip alongside #11426 and #11440 — a fresh npm install of all three combined (2437 packages, 0 vulnerabilities) plus full-suite validation:
- typecheck:core, file-size, changelog-integrity, complexity, cognitive-complexity, check:cycles — all OK, including through the @types/node 22→26 major jump
- npm run lint — 0 errors (after also draining an unrelated stale-suppressions cascade, see #11596)
- npm run test:vitest — 451/452 pass; the 1 failure (auto/glm materialization) is a pre-existing timing-flaky test, reproduced 11/11 pass ×3 in isolation, unrelated to this bump
- Node runtime unaffected — v24.16.0 unchanged, only the type definitions moved

8 development-group updates. The bun 1.3.14→1.4.0 + @types/bun bump aligns with the Bun-native infrastructure work merged earlier today (#11468/#11470/#11471/#11482), which was built against Bun 1.4+ assumptions.
muhamadgalihsaputra pushed a commit to niyatna/NiyatnaRoute that referenced this pull request Sep 27, 2026
…heaper-inference#11468)

Validated in a combined 4-PR batch worktree off release/v3.8.51 tip (Bun-native SQLite infrastructure cluster from the same contributor).
- Focused test: bunSqliteAdapter.test.ts — part of batch's 5/5 node:test run
- typecheck:core, file-size, changelog-integrity, complexity, cognitive-complexity — all OK
- Full-repo lint: 228 pre-existing dashboard react-hooks/* findings, unrelated to this diff

Thanks for tracing this to the pre-boot better-sqlite3 require — a native NAPI abort that JS try/catch cannot recover from is exactly the kind of failure mode that needs the guard moved earlier, and aligning bootstrap/sync-env with the already-preferred bun:sqlite driver is the right fix.
muhamadgalihsaputra pushed a commit to niyatna/NiyatnaRoute that referenced this pull request Sep 27, 2026
…heaper-inference#11470)

Validated in a combined 4-PR batch worktree off release/v3.8.51 tip (Bun-native SQLite infrastructure cluster; complements cheaper-inference#11468 already merged).
- typecheck:core, file-size, changelog-integrity, complexity, cognitive-complexity — all OK (Docker build itself not executed in this environment; reviewed the Dockerfile.bun diff for correctness)
- Full-repo lint: 228 pre-existing dashboard react-hooks/* findings, unrelated to this diff

Thanks for the two concrete failure modes fixed (missing postinstall.mjs during layer-cached bun install, ARM64 NAPI crash avoided by using the native bun:sqlite smoke check instead of rebuilding better-sqlite3) plus the non-root USER bun hardening.
muhamadgalihsaputra pushed a commit to niyatna/NiyatnaRoute that referenced this pull request Sep 27, 2026
…nce#11482)

Validated in a combined 4-PR batch worktree off release/v3.8.51 tip (complements cheaper-inference#11468 and cheaper-inference#11470, both merged first). Conflicted against cheaper-inference#11470's Dockerfile.bun hunk in the shared worktree — resolved additively (both the ownership hardening and the addon-stripping RUN block coexist, ordered so the strip runs as root before USER bun) and pushed the same resolution to this branch.
- Focused test: bun-runtime-native-addon.test.mjs — 1/1 pass, part of batch's 5/5 node:test run
- typecheck:core, file-size, changelog-integrity, complexity, cognitive-complexity — all OK
- Full-repo lint: 228 pre-existing dashboard react-hooks/* findings, unrelated to this diff

Thanks for the isolated-probe root-causing (better-sqlite3 specifically, ruling out keytar/onnxruntime-node/sqlite-vec/tls-client-node/wreq-js/sharp) plus the production ARM64 evidence.
muhamadgalihsaputra pushed a commit to niyatna/NiyatnaRoute that referenced this pull request Sep 27, 2026
…heaper-inference#11428)

Validated in a combined dependabot worktree off release/v3.8.51 tip alongside cheaper-inference#11426 and cheaper-inference#11440 — a fresh npm install of all three combined (2437 packages, 0 vulnerabilities) plus full-suite validation:
- typecheck:core, file-size, changelog-integrity, complexity, cognitive-complexity, check:cycles — all OK, including through the @types/node 22→26 major jump
- npm run lint — 0 errors (after also draining an unrelated stale-suppressions cascade, see cheaper-inference#11596)
- npm run test:vitest — 451/452 pass; the 1 failure (auto/glm materialization) is a pre-existing timing-flaky test, reproduced 11/11 pass ×3 in isolation, unrelated to this bump
- Node runtime unaffected — v24.16.0 unchanged, only the type definitions moved

8 development-group updates. The bun 1.3.14→1.4.0 + @types/bun bump aligns with the Bun-native infrastructure work merged earlier today (cheaper-inference#11468/cheaper-inference#11470/cheaper-inference#11471/cheaper-inference#11482), which was built against Bun 1.4+ assumptions.
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.

3 participants