Repository navigation
fix(browser-sqlite): reject pending requests on OPFS close - #2213
Conversation
🦋 Changeset detectedLatest commit: 9b653d1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
More templates
@tanstack/angular-db
@tanstack/browser-db-sqlite-persistence
@tanstack/capacitor-db-sqlite-persistence
@tanstack/cloudflare-durable-objects-db-sqlite-persistence
@tanstack/db
@tanstack/db-ivm
@tanstack/db-sqlite-persistence-core
@tanstack/electric-db-collection
@tanstack/electron-db-sqlite-persistence
@tanstack/expo-db-sqlite-persistence
@tanstack/node-db-sqlite-persistence
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/react-native-db-sqlite-persistence
@tanstack/react-router-with-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/tauri-db-sqlite-persistence
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
Close now memoizes its in-flight promise, so concurrent close() calls share one close request and outcome instead of the second rejecting with AbortError. Requests submitted after close starts are refused without reaching the worker, which had already cleared its database state. Disposal now takes the rejection reason and settles pending work itself, replacing the reject-then-dispose pair repeated in six handlers. The pinned close table becomes a generated lane with an independent model and fixed, random, and SEED/PATH campaigns. It varies responses answered during close, release order, close outcome, concurrent close, and admission during close, and asserts exact request reach. Rejecting at close start, posting a second close, admitting during close, deferring rejection, the previous fix, and the pre-fix hang each fail it. Co-authored-by: Isaac <no-reply@databricks.com>
The worker handled each message concurrently. When an execute waited for the OPFS access handle (OPFSCoopSyncVFS returns SQLITE_BUSY and wa-sqlite retries after the Web Lock), a close message ran during the wait: SQLite refused to close the unfinalized statement, close was answered before the earlier execute, and the execute resumed on closed VFS files. The worker now handles one request at a time, so close waits for earlier requests. A failed reply is rethrown as an uncaught worker error, not left to stall the queue. Add a request-order oracle that runs the real worker module and wa-sqlite WASM in Node over an in-memory VFS with OPFSCoopSyncVFS's lock protocol, against a sequential replay model. The concurrent worker and a worker that queues executes but not close both fail it. The page lifecycle close lane now also crosses pagehide, Worker error, and messageerror before the close response; ignoring either while close is in flight fails it. Co-authored-by: Isaac <no-reply@databricks.com>
…write The page takes the OPFS access handle through OPFSCoopSyncVFS's own handoff (notify the handle channel, hold its Web Lock), starts an insert that waits behind it, then closes. With the serialized worker the insert and close succeed in order, work after close is refused, and the row survives a reopen. The previous concurrent worker fails it: the insert rejects with AbortError and close with "unable to close due to unfinalized statements". Record the review's calibration controls and remaining limits in docs/contributing/oracle-reviews/pr-2213-opfs-close.md. Co-authored-by: Isaac <no-reply@databricks.com>
A browser OPFS connection refused requests made after close with InvalidPersistedCollectionConfigError, which names a configuration problem. It now rejects them with SQLiteConnectionClosedError, exported from the core and browser packages. Work accepted before close and then cancelled still rejects with AbortError, the same as pagehide. The lifecycle oracle's closed-connection observation and the Chromium witness now expect the new class. The review record documents immediate driver close as the accepted contract. Co-authored-by: Isaac <no-reply@databricks.com>
…hromium Node and Chromium check the same law, so they now share one oracle module with the law, the sequential replay model, the fixed examples, the public-connection driver loop, and the comparison. Each environment supplies only a driver: how to open a connection and how to take and return the OPFS access handle. The Node driver now runs the public connection against the real worker module through a bridge Worker, instead of posting raw worker messages. Its VFS files outlive a connection, so both drivers check that answered writes survive a reopen. The Chromium driver runs all 32 fixed examples against real OPFS, which replaces the single fixed Chromium history. Calibration found that the shared driver returned the handle before close reached the worker, which hid the bug from both drivers. The driver now waits for delivery first. The concurrent worker and a worker that queues executes but not close then fail both drivers. Co-authored-by: Isaac <no-reply@databricks.com>
…#2216) * docs(oracles): treat environments as drivers of one oracle ORC-014 described a real-provider receiving witness as something that "may live in different tests", which reads as permission for a separate test with its own expected results. State instead that when a controlled and a real environment check the same law, the receiving witness is another driver of the same oracle and reuses its law, model, histories, and comparison. A witness with its own expectations is a second model and needs an ORC-011 justification. Point to the shared-driver fairness oracle as the layout to follow, and add the question to the review card. Add a "Treat driver timing helpers as premises" section. A shared driver returned a lock before close reached the worker, so both worker mutants passed both drivers until the helper waited for delivery. Make the oracle-authoring trigger explicit for oracle edits made while fixing a bug or a review finding, in AGENTS.md and in both oracle skills. Co-authored-by: Isaac <no-reply@databricks.com> * docs(oracles): apply review fixes to the shared-driver guidance Use the OPFS request-order oracle as the example of one oracle with Node and Chromium production drivers. The previous fairness example restated its own expected results in the Chromium spec, which the new rule forbids. Scope the second-model clause inside ORC-014's shared-law case. A receiving witness that states its own expected results must derive them from the oracle's model or record why it cannot and the drift risk. An independent second model needs ORC-011's shared-fault hypothesis. This removes a contradiction between the bullet and the prose, and it leaves different-law handoffs unchanged. Require the timing-helper mutant to be rejected and link ORC-006. State that a fixed-time wait still needs one of the two checks, and link the PR #2213 review record as the recorded case. Use the glossary term "production driver" throughout. Extend the oracle-authoring trigger to edits of an oracle's production driver, history grammar, or comparison. Co-authored-by: Isaac <no-reply@databricks.com> * docs(oracles): describe the request-order drivers accurately The example said the drivers supply only an entry point and access-handle control. They also supply database names and result reporting, and the Node driver generates random histories. The Chromium spec also checks page errors and result count. What matters for the rule still holds: neither driver nor the spec states expected results. Co-authored-by: Isaac <no-reply@databricks.com> --------- Co-authored-by: Isaac <isaac@example.com> Co-authored-by: Isaac <no-reply@databricks.com>
|
🎉 This PR has been released! Backfilled notification for published release candidates. |
🎯 Changes
Before this PR,
close()on a browser OPFS database could leave queries unsettled forever. The connection terminated its worker, so the responses for those queries could never arrive. A caller that waited for that work, for example a transaction or a cleanup step, also waited forever.Now every request that the connection accepts settles exactly once. Close also behaves as one ordered step in the connection's work:
execute()rejects at once with the newSQLiteConnectionClosedErrorand does not reach the worker.close()calls share one close request and one outcome.Pagehide and worker failures do not change. They terminate the worker at once and reject pending work. If one of them occurs while close is in flight, close and the pending work reject with that event's error.
Why close needed changes in two layers
The first commit (from @vincentdchn) fixed the connection side. When close settled, it rejected the remaining requests with
AbortError, the same as pagehide. Review found two more problems in that layer:close()posted its own close request. The first close then rejected it.execute()submitted during close still went to the worker. The worker had already cleared its database state, so the error type depended on timing.The connection now keeps one in-flight close promise and refuses new work while it closes. Disposal also takes the rejection reason and settles all pending work itself. This change removed a reject-then-dispose pair that six handlers repeated.
The second problem was in the worker. It handled each message concurrently. With OPFSCoopSyncVFS, a statement can wait for the OPFS access handle when another tab holds it. During that wait, the worker handled a close message. SQLite then refused to close the connection, because the waiting statement was not finalized:
The worker now handles one request at a time. The browser driver already sends one request at a time through its own queue, so persistence traffic loses no parallelism. The order changes only for direct
database.execute()callers and forclose(). If a reply cannot be posted, the worker reports an uncaught error. The connection treats that error as terminal, so the failure does not block later requests.New error class
SQLiteConnectionClosedErroris new in@tanstack/db-sqlite-persistence-core, and the browser package also exports it. It replacesInvalidPersistedCollectionConfigErrorfor requests made after close starts. That old class described a configuration problem, not a closed connection. Code that usedinstanceof InvalidPersistedCollectionConfigErrorto detect a closed connection must change to use the new class. Work that the connection accepted before close and then cancelled still rejects withAbortError.Limits and trade-offs
close()can now wait while another tab holds the database's OPFS access handle. Pagehide still ends the connection at once. Close has no timeout.BrowserWASQLiteDriver.close()does not wait for queued driver work, the same as the Node and OP-SQLite drivers. An open transaction rolls back, and queued work rejects with the closed-connection error. If driver close waited for queued work, it would wait for a transaction callback's application code, and that code can itself wait for close.sqlite3_open_v2error. This PR did not investigate that limit. The review record lists it.🔬 Oracle Coverage
Two oracles check this change. Each oracle has its own law.
Connection close lane (
tests/opfs-page-lifecycle-oracle.test.ts). The law is the connection's promise contract: every accepted request settles once, and close ends admission. This lane replaces the first commit's fixed table with generated histories. It has an independent model and fixed, random, and SEED/PATH replay campaigns. The histories vary these dimensions:The driver uses a controlled worker that can answer in any order. Thus the lane also covers a close response that arrives before pending responses. The lane checks exact settlements at close settlement and the exact requests that reach the worker.
Request-order oracle (
tests/opfs-request-order-oracle.ts, new). The law is that the connection settles requests in acceptance order with sequential results. Close settles after them, and answered writes survive a reopen. One module holds the law, a sequential replay model, the fixed examples, the driver loop on the public connection, and the comparison. Two drivers run it:tests/opfs-request-order-oracle.test.ts) runs the real worker module and the real wa-sqlite WASM build behind a bridge Worker. Its in-memory VFS has OPFSCoopSyncVFS's lock protocol. It runs fixed, random, and SEED/PATH campaigns.e2e/opfs-request-order-oracle.opfs.spec.ts) runs all 32 fixed examples against real OPFS. It takes the access handle through the VFS's own BroadcastChannel and Web Lock handoff. CI runs it in thetest:opfs-fairnessjob.Each control below fails at least one oracle with an assertion, not a timeout:
At first, the shared driver returned the access handle before close reached the worker. Both worker mutants then passed both drivers. The driver now waits for delivery before it returns the handle.
docs/contributing/oracle-reviews/pr-2213-opfs-close.mdrecords the controls and the limits. The coverage map links both oracles.The evidence does not cover browsers other than Chromium, frozen tabs, a crash between a write and close, or more than one other handle holder.
✅ Checklist
pnpm test.Local checks on the final head:
@tanstack/browser-db-sqlite-persistenceunit and type suite: 20 files, 467 tests passed, no type errors.test:opfs-fairnessChromium OPFS config, including the request-order Chromium driver: 39 passed.@tanstack/db-sqlite-persistence-coretypecheck passed. Changed-file lint and formatting passed.The full repository
pnpm testdid not run locally on the final head. The PR description history keeps the local checks for the first commit.🚀 Release Impact
Summary by CodeRabbit
New Features
SQLiteConnectionClosedErrorfor requests made after connection closure begins.Bug Fixes
Documentation
Tests
This pull request and its description were written by Isaac.