@@ -36,6 +36,7 @@ class SqliteConnectionImpl
3636 final bool readOnly;
3737
3838 final bool profileQueries;
39+ bool _didOpenSuccessfully = false ;
3940
4041 SqliteConnectionImpl ({
4142 required AbstractDefaultSqliteOpenFactory openFactory,
@@ -47,11 +48,11 @@ class SqliteConnectionImpl
4748 bool primary = false ,
4849 }) : _writeMutex = mutex,
4950 profileQueries = openFactory.sqliteOptions.profileQueries {
50- isInitialized = _isolateClient.ready;
5151 this .upstreamPort = upstreamPort ?? listenForEvents ();
5252 // Accept an incoming stream of updates, or expose one if not given.
5353 this .updates = updates ?? updatesController.stream;
54- _open (openFactory, primary: primary, upstreamPort: this .upstreamPort);
54+ isInitialized =
55+ _open (openFactory, primary: primary, upstreamPort: this .upstreamPort);
5556 }
5657
5758 Future <void > get ready async {
@@ -100,22 +101,26 @@ class SqliteConnectionImpl
100101 _isolateClient.tieToIsolate (_isolate);
101102 _isolate.resume (_isolate.pauseCapability! );
102103 await _isolateClient.ready;
104+ _didOpenSuccessfully = true ;
103105 });
104106 }
105107
106108 @override
107109 Future <void > close () async {
108110 eventsPort? .close ();
109111 await _connectionMutex.lock (() async {
110- if (readOnly) {
111- await _isolateClient.post (const _SqliteIsolateConnectionClose ());
112- } else {
113- // In some cases, disposing a write connection lock the database.
114- // We use the lock here to avoid "database is locked" errors.
115- await _writeMutex.lock (() async {
112+ if (_didOpenSuccessfully) {
113+ if (readOnly) {
116114 await _isolateClient.post (const _SqliteIsolateConnectionClose ());
117- });
115+ } else {
116+ // In some cases, disposing a write connection lock the database.
117+ // We use the lock here to avoid "database is locked" errors.
118+ await _writeMutex.lock (() async {
119+ await _isolateClient.post (const _SqliteIsolateConnectionClose ());
120+ });
121+ }
118122 }
123+
119124 _isolate.kill ();
120125 });
121126 }
0 commit comments