Skip to content

Fix unrecoverable error when opening native db #95

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 1 commit into from
May 28, 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 @@ -30,12 +30,10 @@ class ParentPortClient implements PortClient {
ParentPortClient() {
final initCompleter = Completer<SendPort>.sync();
sendPortFuture = initCompleter.future;
sendPortFuture.then((value) {
sendPort = value;
});
_receivePort.listen((message) {
if (message is _InitMessage) {
assert(!initCompleter.isCompleted);
sendPort = message.port;
initCompleter.complete(message.port);
} else if (message is _PortChannelResult) {
final handler = handlers.remove(message.requestId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class SqliteConnectionImpl
final bool readOnly;

final bool profileQueries;
bool _didOpenSuccessfully = false;

SqliteConnectionImpl({
required AbstractDefaultSqliteOpenFactory openFactory,
Expand All @@ -47,11 +48,11 @@ class SqliteConnectionImpl
bool primary = false,
}) : _writeMutex = mutex,
profileQueries = openFactory.sqliteOptions.profileQueries {
isInitialized = _isolateClient.ready;
this.upstreamPort = upstreamPort ?? listenForEvents();
// Accept an incoming stream of updates, or expose one if not given.
this.updates = updates ?? updatesController.stream;
_open(openFactory, primary: primary, upstreamPort: this.upstreamPort);
isInitialized =
_open(openFactory, primary: primary, upstreamPort: this.upstreamPort);
}

Future<void> get ready async {
Expand Down Expand Up @@ -100,22 +101,26 @@ class SqliteConnectionImpl
_isolateClient.tieToIsolate(_isolate);
_isolate.resume(_isolate.pauseCapability!);
await _isolateClient.ready;
_didOpenSuccessfully = true;
});
}

@override
Future<void> close() async {
eventsPort?.close();
await _connectionMutex.lock(() async {
if (readOnly) {
await _isolateClient.post(const _SqliteIsolateConnectionClose());
} else {
// In some cases, disposing a write connection lock the database.
// We use the lock here to avoid "database is locked" errors.
await _writeMutex.lock(() async {
if (_didOpenSuccessfully) {
if (readOnly) {
await _isolateClient.post(const _SqliteIsolateConnectionClose());
});
} else {
// In some cases, disposing a write connection lock the database.
// We use the lock here to avoid "database is locked" errors.
await _writeMutex.lock(() async {
await _isolateClient.post(const _SqliteIsolateConnectionClose());
});
}
}

_isolate.kill();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class SqliteDatabaseImpl

@override
@protected
// Native doesn't require any asynchronous initialization
late Future<void> isInitialized = Future.value();
// ignore: invalid_use_of_protected_member
late Future<void> isInitialized = _internalConnection.isInitialized;

late final SqliteConnectionImpl _internalConnection;
late final SqliteConnectionPool _pool;
Expand Down
28 changes: 28 additions & 0 deletions packages/sqlite_async/test/native/basic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,38 @@ void main() {

await Future.wait([f1, f2]);
});

test('reports open error', () async {
// Ensure that a db that fails to open doesn't report any unhandled
// exceptions. This could happen when e.g. SQLCipher is used and the open
// factory supplies a wrong key pragma (because a subsequent pragma to
// change the journal mode then fails with a "not a database" error).
final db =
SqliteDatabase.withFactory(_InvalidPragmaOnOpenFactory(path: path));
await expectLater(
db.initialize(),
throwsA(
isA<Object>().having(
(e) => e.toString(), 'toString()', contains('syntax error')),
),
);
});
});
}

// For some reason, future.ignore() doesn't actually ignore errors in these tests.
void ignore(Future future) {
future.then((_) {}, onError: (_) {});
}

class _InvalidPragmaOnOpenFactory extends DefaultSqliteOpenFactory {
const _InvalidPragmaOnOpenFactory({required super.path});

@override
List<String> pragmaStatements(SqliteOpenOptions options) {
return [
'invalid syntax to fail open in test',
...super.pragmaStatements(options),
];
}
}