Fix race condition in database opening

This commit is contained in:
Simon Binder 2019-09-04 18:38:55 +02:00
parent 1bbc26767e
commit 3c56e0ad6e
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 7 additions and 4 deletions

View File

@ -159,7 +159,7 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.7"
version: "1.1.6"
mime:
dependency: transitive
description:
@ -173,14 +173,14 @@ packages:
path: "../../../moor"
relative: true
source: path
version: "1.6.0"
version: "1.7.1"
moor_flutter:
dependency: "direct main"
description:
path: "../../../moor_flutter"
relative: true
source: path
version: "1.6.0"
version: "1.7.0"
multi_server_socket:
dependency: transitive
description:

View File

@ -232,6 +232,8 @@ class DelegatedDatabase extends QueryExecutor with _ExecutorWithQueryDelegate {
@override
SqlDialect get dialect => delegate.dialect;
final Lock _openingLock = Lock();
DelegatedDatabase(this.delegate,
{this.logStatements, this.isSequential = false}) {
// not using default value because it's commonly set to null
@ -240,7 +242,7 @@ class DelegatedDatabase extends QueryExecutor with _ExecutorWithQueryDelegate {
@override
Future<bool> ensureOpen() {
return _synchronized(() async {
return _openingLock.synchronized(() async {
final alreadyOpen = await delegate.isOpen;
if (alreadyOpen) return true;

View File

@ -53,6 +53,7 @@ class _SqfliteDelegate extends DatabaseDelegate with _SqfliteExecutor {
@override
Future<void> open([GeneratedDatabase db]) async {
print('uses fix for the opening problem.');
assert(
!_debugIsOpening && !isOpen,
'Database opened multiple times, this should never happen. '