Repository navigation
Conversation
db.prepare() returns a statement whose underlying sqlite3_stmt is only released when the JS wrapper is finalized or garbage collected. A library such as drizzle-orm that calls db.prepare() per query and drops the reference leaves those statements alive until GC, so a following db.close(true) failed with "database is locked" (sqlite3_close() returning SQLITE_BUSY) even though every query had completed. VersionSqlite3 now tracks the JSSQLStatement wrappers prepared against it. close() walks that set and finalizes each statement before closing the connection, which is what better-sqlite3's Database#close() does. Using a statement after its database was closed throws "Statement has finalized". Fixes #11418
WalkthroughChangesThe SQLite binding now tracks live prepared statements per connection and finalizes them when the database closes. Cleanup paths deregister statements and prevent double finalization. Tests cover close behavior, dropped statements, disposal, and concurrent operations. SQLite statement lifecycle
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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 `@test/js/bun/sqlite/sqlite.test.js`:
- Around line 1569-1572: Replace the implementation-narrative comment near the
regression test with only the issue URL comment, preserving the existing test
name and test behavior.
- Around line 1566-1580: The test around db.prepare() and close(true) must prove
the dropped statement remains outstanding before closing. Retain a WeakRef to
one prepared statement, ensure its wrapper becomes unreachable as currently
intended, and assert the WeakRef dereferences immediately before db.close(true);
preserve the existing successful close assertion.
🪄 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: 318e10be-93b8-432f-bd6d-208cae4c1a57
📒 Files selected for processing (2)
src/jsc/bindings/sqlite/JSSQLStatement.cpptest/js/bun/sqlite/sqlite.test.js
|
Updated 6:19 AM PT - Jul 25th, 2026
⏳ @robobun, your commit 6f1cd91 is still building in
|
|
This PR may be a duplicate of:
🤖 Generated with Claude Code |
There was a problem hiding this comment.
Beyond the inline findings, I also checked: (1) whether ~JSSQLStatement mutating version_db->statements during GC sweep can race with close() — it can't, JSDestructibleObject destructors run on the mutator thread (same reason the pre-existing non-atomic reference_count is safe); (2) the sqlite3_close() failure path after the swap — the orphaned wrappers have stmt == nullptr, their later removeFirst(this) is a harmless not-found no-op, and release() still balances; (3) the can continue to use existing statements after database has been GC'd test — unaffected because Database is a pure-JS class with no native finalizer, so GC of the wrapper never reaches the new finalize loop.
Extended reasoning...
This PR changes the user-facing contract of Database#close() (statements are now force-finalized rather than allowed to finish or triggering a throw), and the description names two competing open PRs (#33307, #34122) that take different design positions on the same issue — so a human should sign off on the semantics. The inline docs/types nit already flags that sqlite.mdx and sqlite.d.ts still describe the old contract. The note above records the memory-safety concerns I examined and ruled out so they don't need to be re-derived.
… SuppressedError tests
There was a problem hiding this comment.
No new issues — the latest push addressed the docs-Note nit and the comment-cop flags. Deferring to a maintainer because this changes close(false)'s user-visible contract (statements now throw instead of continuing via sqlite3_close_v2 zombie mode), and #33307 / #34122 take narrower approaches to the same #11418 symptom; that's an API-design call I shouldn't make.
What was reviewed:
VersionSqlite3::statementslifecycle — append increate, swap-and-finalize inclose, O(1) swap-remove infinalize()/~JSSQLStatement; no dangling-pointer or double-finalize path found.- Post-
close()GC of the trackedJSSQLStatements:stmtis nulled so the destructor'ssqlite3_finalizeis skipped, anduntrackStatementon the now-empty vector is a no-op. - Updated
#13082test still guards the original segfault while asserting the new throw.
Extended reasoning...
Overview
Native change in src/jsc/bindings/sqlite/JSSQLStatement.cpp: VersionSqlite3 gains a non-owning Vector<JSSQLStatement*> populated in create() and depopulated via a swap-with-last helper in finalize() and ~JSSQLStatement(). jsSQLStatementCloseStatementFunction now swaps the vector out and sqlite3_finalizes every outstanding sqlite3_stmt* before calling sqlite3_close/sqlite3_close_v2. Docs (docs/runtime/sqlite.mdx) and types (packages/bun-types/sqlite.d.ts) are updated to describe the new contract, and test/js/bun/sqlite/sqlite.test.js gains/updates seven tests covering the drizzle repro, using+SuppressedError, WAL file release with a live statement, and the #13082 post-close throw.
All feedback from earlier runs is addressed as of 50da5a1: O(1) untrack (was removeFirst), docs/.d.ts rewritten to match the new semantics, the GC Note reworded to describe the refcount behavior instead of implying close() runs on GC, the WeakRef guard added to the drizzle-pattern test, and both multi-line C++ comments trimmed per comment-cop.
Security risks
None identified. No untrusted input parsing; the new state is a per-connection vector of raw pointers whose add/remove sites are all on the JS thread and gated on the same version_db non-null check, so there's no path that appends without a matching remove. sqlite3_finalize in the close loop doesn't re-enter JS.
Level of scrutiny
Medium-high. The native change is small and mechanically sound, but it flips a documented, tested behavior: close(false) used to defer to sqlite3_close_v2's zombie mode so live statements kept working (the previous #13082 test asserted stmt.all() succeeded after close()); now they throw Statement has finalized. That's the right call per better-sqlite3 parity and fixes real drizzle-orm breakage, but it can break user code that relied on the old zombie semantics, and two other open PRs solve #11418 differently (#33307 keeps close(true) throwing; #34122 only reroutes [Symbol.dispose]). Which contract ships is a maintainer decision.
Other factors
Test coverage is thorough for the new behavior (both close variants, using, WAL file release, dropped-without-finalize + WeakRef liveness proof, #13082 concurrent GC). The GC-keeps-connection-alive test (can continue to use existing statements after database has been GC'd) is unchanged and still passes, confirming the refcount path wasn't touched. CI build #80581 was queued for 53a886c; 50da5a1 is docs/comments-only on top of it.
There was a problem hiding this comment.
Beyond the inline nit, I traced the new VersionSqlite3::statements vector for dangling-pointer and refcount hazards: entries are appended in JSSQLStatement::create, removed in both jsSQLStatementFunctionFinalize and ~JSSQLStatement before the object is freed, and close() swaps the vector into a local before iterating so a re-entrant untrack can't invalidate the loop. The finalized statements keep their version_db ref until GC, and release() on the already-nulled db is a no-op, so the refcount stays balanced. All mutation is JS-thread-only. This is still a user-visible behavior change to close() with two competing open PRs (#33307, #34122), so leaving the approach call to a maintainer.
CI statusBuild #80799:
Earlier builds #80361 and #80622 were pure build timeouts (no test jobs ran). The diff itself is green where tests ran; needs a maintainer to look past the infra noise. All review feedback has been addressed through 50da5a1. |
… query() cache LRU (#36793) Follow-up to #36573, which made both `close()` modes force-finalize every statement. That was a behavior change for `db.prepare()` statements held across `close(false)` (reads/iterators used to keep working via `sqlite3_close_v2`). This restores a coherent graceful mode and gives `throwOnError` real meaning. | after… | `close()` / `close(false)` | `close(true)` / `using` | |---|---|---| | `db.query()` statements (cached or evicted) | finalized | finalized | | `db.prepare()` statements | **keep working** (reads *and* writes) until finalized/GC'd | finalized | | connection / file handle | released when last `prepare()` stmt is finalized (`sqlite3_close_v2`) | released immediately (`sqlite3_close`), throws on failure | | using a finalized stmt | `Database has closed` | `Database has closed` | | `db.query()` / `db.prepare()` new | throws | throws | Also: | | before | after | |---|---|---| | `db.query()` cache | first 20 distinct strings cached forever; everything after uncached and non-`PERSISTENT` | LRU of `MAX_QUERY_CACHE_SIZE`; every `query()` stmt is `PERSISTENT` and tracked as Database-owned so `close()` finalizes it even after eviction | | `prepare()` stmt `.run()` after `close(false)` | `Database has closed` (while `.all()` worked) | works (statement paths use `sqlite3_db_handle(stmt)`) | | post-close error | `Statement has finalized` | `Database has closed` | #36572 / #35604 / #11418 stay fixed (`close(true)` never hits `SQLITE_BUSY`; `close()` releases the file when only `query()` statements are outstanding). Docs and types updated. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Repro
The statement wrappers fell out of scope but have not been garbage collected, so their
sqlite3_stmt*is still live whensqlite3_close()runs and it reportsSQLITE_BUSY. The original report (#11418) hit this viadrizzle-orm/bun-sqlite/migrator, which prepares a statement per migration query.Cause
Database#closeonly finalized the query cache (db.query()results) and thedb.transaction()controller statements (#27202). Statements returned fromdb.prepare()are not tracked anywhere on the JS side, soclose(true)routed straight tosqlite3_close()with unfinalized statements outstanding.Fix
VersionSqlite3now records everyJSSQLStatementprepared against it.close()(both thesqlite3_closeandsqlite3_close_v2paths) walks that set and finalizes each statement before closing the connection, which is what better-sqlite3'sDatabase#closedoes. Using a statement after its database was closed throwsStatement has finalized, the same errorstmt.finalize()already produced.The garbage-collection path is unchanged: a database whose JS object is collected while statements are still reachable keeps the connection open through
VersionSqlite3's refcount until the last statement is finalized (can continue to use existing statements after database has been GC'dstill covers this).This supersedes the behavior asserted by the previous
close(true) should throw an error if the database is in useandshould dispose AND throw an error if the database is in usetests; those now assert the statement is finalized rather than thatclosethrows.Verification
Fixes #11418
Related
#33307 adds the same statement tracking for
close(false)but keepsclose(true)throwing; this PR finalizes in both paths so the drizzle case works. #34122 routes[Symbol.dispose]toclose(false), which this PR also makes unnecessary sinceclose(true)no longer throws for live prepared statements.no test proof · iteration 1 · Platform-specific test(s) that do not run on this machine. Deferring to CI, which covers all platforms: test/js/bun/sqlite/sqlite.test.js