Repository navigation
[WasmFS] Use readwrite-unsafe access handles in the OPFS backend - #27937
Open
JulesPatmanidis wants to merge 5 commits into
Open
JulesPatmanidis wants to merge 5 commits into
JulesPatmanidis wants to merge 5 commits into
Conversation
Contributor
Author
Member
|
Yes, I think this is worth doing. Let's just document the limitations and add a changelog entry so users are aware of the changes. |
Contributor
Author
|
Great! I added a note to I will mark this ready for review when CI passes. |
JulesPatmanidis
marked this pull request as ready for review
October 10, 2026 08:44
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The OPFS backend used exclusive sync access handles for files opened for writing and Blobs for files opened read-only, so that several tabs could read the same file. Reads through a Blob are slow, especially from several threads (#27134), and a file open for writing blocks every other tab and worker.
This change opens all files with
{mode: 'readwrite-unsafe'}when supported by the browser, which allows several handles on the same file. Read-only opens now also use an access handle in that case.Browsers without support for
mode(Firefox and Safari currently) accept the option silently and return an exclusive handle, so we can't tell if it is supported just from the result. Instead, the first open passes an options object with a getter formodeand checks whether the browser read it. If the browser readsmodebut rejects the value with aTypeError, the backend treats it as unsupported.If not supported, write opens use an exclusive handle and read-only opens use a Blob just as before.
This only affects builds with pthreads, since only those use sync access handles.
Behavior changes in browsers that support
mode:EACCES.openwithO_RDONLY | O_TRUNCnow truncates the file as on Linux and in the JS FS. Previously the truncation did not happen because the file was read through a Blob.Also removes the
createSyncAccessHandle.lengthcheck and its{mode: 'in-place'}branch.lengthis 0 in all current browsers because the options argument is optional, so that branch never ran.Tests:
test_wasmfs_opfsexpects theO_RDONLY | O_TRUNCtruncation to succeed in pthreads builds running in Chrome 121 or newer, and to fail as before otherwise.test_wasmfs_opfs_sharedchecks that WasmFS can open a file in every mode while another context holds areadwrite-unsafehandle for it.Open questions
Because read-only opens now hold an access handle, OPFS refuses to remove or move the file while it is open. In Chromium with this change,
unlinkorrenameon a file that has an open read-only fd fails withEIO. Previously this only happened for files open for writing.A related, smaller effect: a read-only open now holds a lock that blocks contexts needing an exclusive handle, for example, a
truncateon a file with no open fd in another tab (which usescreateWritable).Is this a reasonable limitation that can just be documented (this happens anyway for files open for writing), or do you prefer a different approach?
readwrite-unsafesync access handles in pthreads builds if supported by the browser. Files can then be opened by several tabs or workers at the same time, and files opened read-only are read much faster from multiple threads."Fixes #21869
Should help with #27134