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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:sqlite_async/src/common/sqlite_database.dart';
import 'package:sqlite_async/src/sqlite_connection.dart';
import 'package:sqlite_async/src/sqlite_options.dart';
import 'package:sqlite_async/src/update_notification.dart';
import 'package:sqlite_async/src/web/web_mutex.dart';
import 'package:sqlite_async/src/web/web_sqlite_open_factory.dart';
import 'package:sqlite_async/web.dart';

Expand Down Expand Up @@ -43,7 +42,6 @@ class SqliteDatabaseImpl
@override
AbstractDefaultSqliteOpenFactory openFactory;

late final Mutex mutex;
late final WebDatabase _connection;
StreamSubscription? _broadcastUpdatesSubscription;

Expand Down Expand Up @@ -77,15 +75,15 @@ class SqliteDatabaseImpl
/// 4. Creating temporary views or triggers.
SqliteDatabaseImpl.withFactory(this.openFactory,
{this.maxReaders = SqliteDatabase.defaultMaxReaders}) {
mutex = MutexImpl();
// This way the `updates` member is available synchronously
updates = updatesController.stream;
isInitialized = _init();
}

Future<void> _init() async {
_connection = await openFactory.openConnection(SqliteOpenOptions(
primaryConnection: true, readOnly: false, mutex: mutex)) as WebDatabase;
_connection = await openFactory.openConnection(
SqliteOpenOptions(primaryConnection: true, readOnly: false))
as WebDatabase;

final broadcastUpdates = _connection.broadcastUpdates;
if (broadcastUpdates == null) {
Expand Down
4 changes: 4 additions & 0 deletions packages/sqlite_async/test/basic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ void main() {
);

await completion;
}, onPlatform: {
'browser': Skip(
'Web locks are managed with a shared worker, which does not support timeouts',
)
});
});
}
Expand Down