Repository navigation
sqlite: add AsyncDatabase - #34863
ninjaasmoke wants to merge 11 commits into
Conversation
schedule native-only SQLite work on WorkPool and return completion by ScriptExecutionContext identifier. keep VM-owned state on the JS thread and safely abandon completion during Worker teardown.
WalkthroughThe change adds Bun’s asynchronous SQLite API, including native worker execution, FIFO scheduling, cancellation, lifecycle management, typed declarations, documentation, comprehensive tests, and benchmarks comparing event-loop and HTTP availability. ChangesAsynchronous SQLite API
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@bench/sqlite/http-availability.js`:
- Line 2: Update the serverPath initialization to convert the file URL with
fileURLToPath() rather than reading URL.pathname, ensuring the resulting path is
OS-correct for Bun.spawn’s cmd invocation. Add or reuse the appropriate
URL-to-path import while preserving the existing http-availability server
target.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 79a1d644-c8fa-4e20-9033-f46d8f9fc134
📒 Files selected for processing (23)
bench/sqlite/async-availability-worker.jsbench/sqlite/async-availability.jsbench/sqlite/http-availability-server.jsbench/sqlite/http-availability.jsdocs/runtime/sqlite.mdxpackages/bun-types/sqlite.d.tsscripts/build/unified.tssrc/js/builtins/BunBuiltinNames.hsrc/js/bun/sqlite.tssrc/js/internal-for-testing.tssrc/jsc/AsyncSQLiteTask.rssrc/jsc/bindings/ErrorCode.tssrc/jsc/bindings/ZigGlobalObject.cppsrc/jsc/bindings/sqlite/AsyncSQLiteDatabase.cppsrc/jsc/bindings/sqlite/AsyncSQLiteDatabase.hsrc/jsc/bindings/sqlite/JSSQLStatement.cppsrc/jsc/bindings/sqlite/JSSQLStatement.hsrc/jsc/bindings/sqlite/lazy_sqlite3.hsrc/jsc/bindings/webcore/DOMClientIsoSubspaces.hsrc/jsc/bindings/webcore/DOMIsoSubspaces.hsrc/jsc/lib.rstest/integration/bun-types/fixture/sqlite.tstest/js/bun/sqlite/sqlite.test.js
|
A note on one overlap with #43281.
#43281 changes the sync path to |
What does this PR do?
Fixes #978
bun:sqliteexecutes every statement on the calling thread. A query that runslong — a big aggregate, a slow join — blocks the isolate's event loop for its
full duration, and wrapping the call in a Promise would not change that, since
the work still runs where it was called.
This adds
AsyncDatabase, a separate connection type that runs the SQLite workon Bun's WorkPool. It opens its own
sqlite3*and shares no handle, cache, orstatement with
Database. The synchronousDatabase/Statementpath isunchanged — no async branch, lock, or allocation was added to it. The surface is
the usual
open/exec/run/get/all/values/closeplus async disposal,with per-operation
AbortSignalandstrict/safeIntegersoptions.Each async connection owns one
sqlite3*, a bounded FIFO, and anactive-operation ID; only the FIFO head runs at a time. Bindings are copied off
JS values before admission, and rows and errors are copied out of SQLite before
finalize, so a WorkPool job holds no Promise,
JSValue, VM, global object, orevent-loop pointer. Completion is routed back by
ScriptExecutionContextIdentifier, which is what lets an operation outlive aforce-terminated Worker: the VM can be gone before the job finishes, and the job
never dereferences it. Cancellation removes a queued operation by ID, or
interrupts a running one only while that same ID owns the active slot. Close
fences admission, drains accepted work, and closes once.
The
bun:sqlite(173) andnode:sqlite(112) suites pass unchanged, alongsideASAN, VM-destruction, Worker-teardown, GC, and close/cancel-race checks.
Availability, five-run release medians — one slow query while 100
/pingrequests hit the same server:
DatabaseAsyncDatabaseSame query time; the sync server completes zero pings before it returns, the
async server all 100.
Full technical write-up: Async SQLite design and performance
Note: Parts of this PR were co-authored with GitHub Copilot (GPT-5.6).
How did you verify your code works?
The full
bun:sqlitesuite was run under debug ASAN with JSC exception checks and forced VM destruction:BUN_JSC_validateExceptionChecks=1 \ BUN_JSC_dumpSimulatedThrows=1 \ BUN_DESTRUCT_VM_ON_EXIT=1 \ bun bd -j2 test test/js/bun/sqlite/sqlite.test.js --timeout 60000 173 passed, 0 failed.The new coverage includes FIFO ordering, queue limits, copied bindings, open and close races, GC, natural and explicit process exit, running and queued cancellation, cancellation versus close, lock contention, result ownership, and repeated Worker termination with active and queued work.
The existing compatibility and type suites also pass:
• node:sqlite : 112 passed, 4 expected skips, 0 failed
• bun-types: 13 passed, 0 failed
• rust:check and rust:check-all
• Darwin lazy-SQLite compile path
• LLVM 21 C++ formatting, Rust formatting, Prettier, and git diff --check
The availability numbers above come from five runs of the non-LTO release build. The synchronous and async servers execute the same recursive query and validate the same result.