Skip to content

OPFSAdaptiveVFS: retry a held access handle after a terminated connection - #382

Merged
rhashimoto merged 1 commit into
rhashimoto:masterfrom
lalexdotcom:opfsadaptive-retry-held-handle
Oct 23, 2026
Merged

rhashimoto merged 1 commit into
rhashimoto:masterfrom
lalexdotcom:opfsadaptive-retry-held-handle

Conversation

@lalexdotcom

Copy link
Copy Markdown
Contributor

What happens

On platforms without readwrite-unsafe access handles (Firefox and Safari), OPFSAdaptiveVFS hands its access handle between connections through a Web Lock. When a connection's worker is terminated while it holds the handle, the browser releases the lock at once but the access handle only later. The connection granted the lock next then gets NoModificationAllowedError (InvalidStateError in WebKit) from createSyncAccessHandle() in jLock(), which has no try. The exception leaves the SQLite call raw, and the connection keeps the lock with no handle: with the asyncify build its query never settles and every other connection waits on the lock; with JSPI the query fails, the next one returns SQLITE_IOERR, and close() never returns.

This reproduces in Firefox, most reliably on Windows.

The change

Once jLock() holds the lock, a NoModificationAllowedError or InvalidStateError from createSyncAccessHandle() is retried every 10 ms for up to 5 s. Any other error, or the deadline, gives the lock back and returns SQLITE_IOERR_LOCK, so a connection never holds the lock without the handle.

The cost: a file held outside the lock protocol now makes jLock() fail after 5 s instead of immediately.

The test

test/vfs_terminated_holder.js, run for OPFSAdaptiveVFS: 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. It runs 10 rounds per build, each bounded and cleaned up so a failure cannot spill into the next test.

It fails on master in Firefox and passes with the change. It cannot fail in Chrome, which has readwrite-unsafe and never takes this path, so CI here does not exercise it. In Safari it did not reproduce: a worker running in WebAssembly is not terminated there until it yields.

Relation to #381

This is the OPFSAdaptiveVFS side of #381, which said this VFS was being looked into. Both VFS hand their access handle over through a Web Lock (here only without readwrite-unsafe), so both meet the same race: a terminated holder releases the lock before its handle. The change is the same: once the lock is held, retry createSyncAccessHandle() on NoModificationAllowedError or InvalidStateError, every 10 ms for up to 5 s, with the same constant names. The test is the same shape too. There are two differences:

  • What the race leaves behind. In OPFSCoopSyncVFS the failed hand-over only failed the statement. Here, the connection also kept the lock without a handle, which stalls every other connection. So on failure this change also gives the lock back, and returns SQLITE_IOERR_LOCK where OPFSCoopSyncVFS rethrows from its retry operation.
  • Where it shows. OPFSCoopSyncVFS hands over everywhere, so OPFSCoopSyncVFS: retry access handles still held after a terminated connection #381 reproduces in Chrome too. OPFSAdaptiveVFS takes this path only in Firefox and Safari, so this test cannot fail in Chrome or in CI here.

The two tests do not touch the same lines: #381's is in test/vfs_handover.js, this one in its own file. Whichever merges second, I can move both onto one shared helper if you prefer.

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.

…tion

Without readwrite-unsafe access handles, a connection whose worker is
terminated mid-call releases its Web Lock at once and its access handle
only later. The connection granted the lock next then failed
createSyncAccessHandle() in jLock() with NoModificationAllowedError
(InvalidStateError in WebKit): the exception left the SQLite call raw,
and the connection kept the lock with no handle, which left every other
connection waiting on it. Creating the handle is now retried for up to
5 s while the lock is held; on failure the lock is given back and
jLock() returns SQLITE_IOERR_LOCK.

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! LGTM, but I'm going to wait until you're done with #381 to merge in case you see anything while you're making changes there that might also apply here.

@lalexdotcom

Copy link
Copy Markdown
Contributor Author

I went over #381's review for this one. Here the access handle is a single acquisition in a try/catch, with nothing else to undo and no Promise.allSettled search to type, so neither change applies. Nothing to add on my side.

@rhashimoto
rhashimoto merged commit 010eeb5 into rhashimoto:master Oct 23, 2026
1 check passed
@lalexdotcom
lalexdotcom deleted the opfsadaptive-retry-held-handle branch October 26, 2026 08:30
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