Skip to content

OPFSCoopSyncVFS: retry access handles still held after a terminated connection - #381

Merged
rhashimoto merged 5 commits into
rhashimoto:masterfrom
lalexdotcom:opfscoopsync-retry-held-handle
Oct 23, 2026
Merged

rhashimoto merged 5 commits into
rhashimoto:masterfrom
lalexdotcom:opfscoopsync-retry-held-handle

Conversation

@lalexdotcom

Copy link
Copy Markdown
Contributor

What happens

When a connection's worker is terminated in the middle of a call, the browser releases its Web Lock at once but its access handles only some time later. The connection queued on that lock is granted it, calls createSyncAccessHandle(), and gets NoModificationAllowedError (InvalidStateError in WebKit) because the file is still held. #requestAccessHandle() rethrows it, and retry() lets it out of the SQLite call as a raw exception: the statement fails with a DOM error and no SQLite result code.

This reproduces in Chrome, Firefox and Safari, most reliably in Firefox, which takes longer to release the file, especially on Windows.

The change

Once #requestAccessHandle() holds the lock, a NoModificationAllowedError or InvalidStateError from createSyncAccessHandle() is retried: the handles that attempt got are closed, the lock is kept, and the attempt is repeated every 10 ms for up to 5 s, well above the time the browsers take to release the file. Any other error is thrown at once, as before.

The cost: a file held outside the lock protocol (for example by another page's handle that never lets go) now fails after 5 s instead of immediately. The open path still reports it as SQLITE_CANTOPEN with the same lastError.

OPFSAdaptiveVFS uses the same lock-and-notify hand-over where readwrite-unsafe is not available and may have the same problem; we are looking into it separately.

Test

test/vfs_handover.js gains a test: connection B opens a database, connection A opens it and starts a long query, B starts a query and queues on the lock, then A's worker is terminated directly, so the test does not depend on what TestContext.destroy() does first. It runs 10 rounds per build, each bounded and cleaned up so a failure cannot spill into the next test.

On master, B's query fails with the raw NoModificationAllowedError / InvalidStateError in Chrome, Firefox (Linux and Windows) and Safari 26 and 27, on every build where the race came up. With the change, OPFSCoopSyncVFS.test.js passes on all of them, and the whole suite passes on Chrome.

Checklist

  • I grant to recipients of this Project distribution a perpetual,
    non-exclusive, royalty-free, irrevocable copyright license to reproduce, prepare
    derivative works of, publicly display, sublicense, and distribute this
    Contribution and such derivative works.
  • I certify that I am legally entitled to grant this license, and that this
    Contribution contains no content requiring a license from any third party.

lalexdotcom and others added 4 commits October 15, 2026 07:45
…onnection

A connection whose worker is terminated mid-call releases its Web Lock
at once and its access handles only later. The connection granted the
lock next then failed createSyncAccessHandle() with NoModificationAllowedError
(InvalidStateError in WebKit), which left the SQLite call as a raw
exception. Creating the handles is now retried for up to a second while
the lock is held.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…roy()

The test needs a worker terminated with no chance to close its VFS; a
destroy() that closes it first would no longer reproduce the race.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
On a slow engine a round that failed could outlast the test's timeout
and keep its workers running into the next test, which then met files
still held. Each round now waits a bounded time and cleans up in a
finally; the test takes the suite's timeout.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Measured: a worker terminated inside a JavaScript loop kept its file for
up to 956 ms on Firefox on Windows; 1 s left no margin.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
lalexdotcom added a commit to lalexdotcom/browser-sqlite that referenced this pull request Oct 16, 2026
A worker terminated while it holds OPFS sync access handles frees its
Web Lock before its files, on every engine. OPFSCoopSyncVFS hands its
handle over through that lock and called createSyncAccessHandle() once,
so the next holder's statement failed with a raw DOM error that reaches
the caller with no SQLiteError code. The library terminates a worker
that misses its open timeout, so a client sharing the database could
meet it.

Carried as a pnpm patch until rhashimoto/wa-sqlite#381 merges: its
src/ hunks only, byte for byte the PR head 3c75a3c8.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@rhashimoto rhashimoto left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

Just a couple code polishing suggestions.

Comment thread src/examples/OPFSCoopSyncVFS.js Resolved
Comment thread src/examples/OPFSCoopSyncVFS.js Resolved
Each access handle the attempt gets registers its own undo, so a retry
no longer fetches every related file to test what it holds. The type
guard on the search keeps TypeScript before 5.5, which does not infer it,
from reporting failure.reason as an error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@lalexdotcom

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Both are done in 42bfb90:

  • An attempt is now undone through an onError array of closures. Each one is pushed right after a handle is opened, so the retry no longer fetches and tests every related file.
  • The search has the JSDoc type guard. I could reproduce the false positive with TypeScript 5.3 and 5.4. From 5.5 on, TypeScript infers that type predicate by itself, which is why my editor didn't show it. With the annotation, the file checks clean from 5.3 through 7.0.

The full suite still passes on Chrome, and OPFSCoopSyncVFS.test.js passes on Firefox.

@rhashimoto

Copy link
Copy Markdown
Owner

From 5.5 on, TypeScript infers that type predicate by itself, which is why my editor didn't show it.

I figured that was the case. I use Yarn PnP, which TS 7 doesn't support. I'll try with TS 5.5, though, thanks for that!

@rhashimoto
rhashimoto merged commit 8a8bc2e into rhashimoto:master Oct 23, 2026
1 check passed
@lalexdotcom
lalexdotcom deleted the opfscoopsync-retry-held-handle branch October 23, 2026 18:23
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.

2 participants