Skip to content

Fix a race condition around OPFS databases #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2025
Merged

Fix a race condition around OPFS databases #97

merged 2 commits into from
Jun 3, 2025

Conversation

simolus3
Copy link
Contributor

@simolus3 simolus3 commented Jun 2, 2025

In web_sqlite_open_factory.dart, we create a shared mutex derived from the database path when a database is not accessed through shared workers. This is relevant when serving Flutter apps with COOP + COEP headers, because we're able to use a VFS implementation based on OPFS and dedicated workers in that case (so no shared worker is involved).

Unfortunately, web_sqlite_database.dart allocates its own (anonymous) mutex and passes that as an option to the open factory. This means that the shared mutex isn't used, and two tabs could race to use the database simultaneously, which may cause errors as the VFS runs into blocked sync access handles.

Note: A side-effect of this change is that databases with shared workers have no per-tab/client-side lock. This shouldn't be an issue because the shared worker is supposed to hand out leases as well, but it's something to be aware of. This is another improvement though:

  • Previous behavior: Two tabs have an independent mutex on a connection backed by a shared worker. They can't actually run statements concurrently, but they'd be able to both obtain an exclusive context in parallel 😬
  • New behavior: There's only a single mutex, managed by the shared worker. So two tabs can't obtain an exclusive context at the same time, but there's no timeout for locks.

@simolus3 simolus3 requested a review from stevensJourney June 3, 2025 08:21
@simolus3 simolus3 merged commit 8b71db2 into main Jun 3, 2025
6 checks passed
@simolus3 simolus3 deleted the fix-opfs-race branch June 3, 2025 09:28
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