Skip to content

Commit 5cf85db

Browse files
committed
Slightly better handling of connection initialization errors.
1 parent dd2bce0 commit 5cf85db

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

lib/src/port_channel.dart

+19-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ abstract class PortClient {
1818
class ParentPortClient implements PortClient {
1919
late Future<SendPort> sendPortFuture;
2020
SendPort? sendPort;
21-
ReceivePort receivePort = ReceivePort();
21+
final ReceivePort _receivePort = ReceivePort();
22+
final ReceivePort _errorPort = ReceivePort();
2223
bool closed = false;
2324
int _nextId = 1;
2425

@@ -30,7 +31,7 @@ class ParentPortClient implements PortClient {
3031
sendPortFuture.then((value) {
3132
sendPort = value;
3233
});
33-
receivePort.listen((message) {
34+
_receivePort.listen((message) {
3435
if (message is _InitMessage) {
3536
assert(!initCompleter.isCompleted);
3637
initCompleter.complete(message.port);
@@ -57,6 +58,17 @@ class ParentPortClient implements PortClient {
5758
}
5859
close();
5960
});
61+
_errorPort.listen((message) {
62+
var [error, stackTrace] = message;
63+
print('got an error ${initCompleter.isCompleted} $error');
64+
if (!initCompleter.isCompleted) {
65+
if (stackTrace == null) {
66+
initCompleter.completeError(error);
67+
} else {
68+
initCompleter.completeError(error, StackTrace.fromString(stackTrace));
69+
}
70+
}
71+
});
6072
}
6173

6274
Future<void> get ready async {
@@ -94,20 +106,22 @@ class ParentPortClient implements PortClient {
94106
}
95107

96108
RequestPortServer server() {
97-
return RequestPortServer(receivePort.sendPort);
109+
return RequestPortServer(_receivePort.sendPort);
98110
}
99111

100112
void close() async {
101113
if (!closed) {
102114
closed = true;
103115

104-
receivePort.close();
116+
_receivePort.close();
117+
_errorPort.close();
105118
_cancelAll(const ClosedException());
106119
}
107120
}
108121

109122
tieToIsolate(Isolate isolate) {
110-
isolate.addOnExitListener(receivePort.sendPort, response: _closeMessage);
123+
isolate.addErrorListener(_errorPort.sendPort);
124+
isolate.addOnExitListener(_receivePort.sendPort, response: _closeMessage);
111125
}
112126
}
113127

lib/src/sqlite_connection_impl.dart

+4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ class SqliteConnectionImpl with SqliteQueries implements SqliteConnection {
8888
@override
8989
Future<void> close() async {
9090
await _connectionMutex.lock(() async {
91+
if (closed) {
92+
return;
93+
}
9194
if (readOnly) {
9295
await _isolateClient.post(const _SqliteIsolateConnectionClose());
9396
} else {
@@ -97,6 +100,7 @@ class SqliteConnectionImpl with SqliteQueries implements SqliteConnection {
97100
await _isolateClient.post(const _SqliteIsolateConnectionClose());
98101
});
99102
}
103+
_isolateClient.close();
100104
_isolate.kill();
101105
});
102106
}

test/basic_test.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ void main() {
377377
// 4. Now second connection is ready. Second query has two connections to choose from.
378378
// 5. However, first connection is closed, so it's removed from the pool.
379379
// 6. Triggers `Concurrent modification during iteration: Instance(length:1) of '_GrowableList'`
380-
381380
final db =
382381
SqliteDatabase.withFactory(testFactory(path: path, initStatements: [
383382
// Second connection to sleep more than first connection
@@ -387,8 +386,9 @@ void main() {
387386

388387
final future1 = db.get('SELECT test_sleep(10) as sleep');
389388
final future2 = db.get('SELECT test_sleep(10) as sleep');
390-
final closeFuture = db.close();
391-
await closeFuture;
389+
390+
await db.close();
391+
392392
await future1;
393393
await future2;
394394
});

0 commit comments

Comments
 (0)