fix: prevent database deadlock (eps 2)

This commit is contained in:
Alex
2025-09-08 21:10:51 -05:00
parent 23fb2e0fae
commit 9996a1ec3e
13 changed files with 32 additions and 17 deletions
@@ -246,7 +246,7 @@ Future<void> backgroundSyncNativeEntrypoint() async {
WidgetsFlutterBinding.ensureInitialized();
DartPluginRegistrant.ensureInitialized();
final (isar, drift, logDB) = await Bootstrap.initDB();
final (isar, drift, logDB) = await Bootstrap.initDB(shareAcrossIsolates: false);
await Bootstrap.initDomain(isar, drift, logDB, shouldBufferLogs: false);
await BackgroundWorkerBgService(isar: isar, drift: drift, driftLogger: logDB).init();
}
@@ -66,8 +66,14 @@ class IsarDatabaseRepository implements IDatabaseRepository {
include: {'package:immich_mobile/infrastructure/entities/merged_asset.drift'},
)
class Drift extends $Drift implements IDatabaseRepository {
Drift([QueryExecutor? executor])
: super(executor ?? driftDatabase(name: 'immich', native: const DriftNativeOptions(shareAcrossIsolates: true)));
Drift({QueryExecutor? executor, bool shareAcrossIsolates = true})
: super(
executor ??
driftDatabase(
name: 'immich',
native: DriftNativeOptions(shareAcrossIsolates: shareAcrossIsolates),
),
);
@override
int get schemaVersion => 10;
@@ -7,9 +7,13 @@ import 'logger_db.repository.drift.dart';
@DriftDatabase(tables: [LogMessageEntity])
class DriftLogger extends $DriftLogger implements IDatabaseRepository {
DriftLogger([QueryExecutor? executor])
DriftLogger({QueryExecutor? executor, bool shareAcrossIsolates = true})
: super(
executor ?? driftDatabase(name: 'immich_logs', native: const DriftNativeOptions(shareAcrossIsolates: true)),
executor ??
driftDatabase(
name: 'immich_logs',
native: DriftNativeOptions(shareAcrossIsolates: shareAcrossIsolates),
),
);
@override
+1 -1
View File
@@ -42,7 +42,7 @@ import 'package:worker_manager/worker_manager.dart';
void main() async {
ImmichWidgetsBinding();
final (isar, drift, logDb) = await Bootstrap.initDB();
final (isar, drift, logDb) = await Bootstrap.initDB(shareAcrossIsolates: true);
await Bootstrap.initDomain(isar, drift, logDb);
await initApp();
// Warm-up isolate pool for worker manager
+1 -1
View File
@@ -330,7 +330,7 @@ class BackgroundService {
}
Future<bool> _onAssetsChanged() async {
final (isar, drift, logDb) = await Bootstrap.initDB();
final (isar, drift, logDb) = await Bootstrap.initDB(shareAcrossIsolates: false);
await Bootstrap.initDomain(isar, drift, logDb);
final ref = ProviderContainer(
@@ -115,7 +115,7 @@ class BackupVerificationService {
assert(tuple.deleteCandidates.length == tuple.originals.length);
final List<Asset> result = [];
BackgroundIsolateBinaryMessenger.ensureInitialized(tuple.rootIsolateToken);
final (isar, drift, logDb) = await Bootstrap.initDB();
final (isar, drift, logDb) = await Bootstrap.initDB(shareAcrossIsolates: true);
await Bootstrap.initDomain(isar, drift, logDb);
await tuple.fileMediaRepository.enableBackgroundAccess();
final ApiService apiService = ApiService();
+3 -3
View File
@@ -56,9 +56,9 @@ void configureFileDownloaderNotifications() {
}
abstract final class Bootstrap {
static Future<(Isar isar, Drift drift, DriftLogger logDb)> initDB() async {
final drift = Drift();
final logDb = DriftLogger();
static Future<(Isar isar, Drift drift, DriftLogger logDb)> initDB({required bool shareAcrossIsolates}) async {
final drift = Drift(shareAcrossIsolates: shareAcrossIsolates);
final logDb = DriftLogger(shareAcrossIsolates: shareAcrossIsolates);
Isar? isar = Isar.getInstance();
+1 -1
View File
@@ -34,7 +34,7 @@ Cancelable<T?> runInIsolateGentle<T>({
BackgroundIsolateBinaryMessenger.ensureInitialized(token);
DartPluginRegistrant.ensureInitialized();
final (isar, drift, logDb) = await Bootstrap.initDB();
final (isar, drift, logDb) = await Bootstrap.initDB(shareAcrossIsolates: true);
await Bootstrap.initDomain(isar, drift, logDb, shouldBufferLogs: false);
final ref = ProviderContainer(
overrides: [
@@ -297,7 +297,9 @@ class _SyncStatusIndicatorState extends ConsumerState<_SyncStatusIndicator> with
Widget build(BuildContext context) {
final syncStatus = ref.watch(syncStatusProvider);
final isSyncing = syncStatus.isRemoteSyncing || syncStatus.isLocalSyncing;
print(
"SyncStatusIndicator build - syncStatus.isRemoteSyncing: ${syncStatus.isRemoteSyncing} , isLocalSyncing: ${syncStatus.isLocalSyncing}, isHashing: ${syncStatus.isHashing}",
);
// Control animations based on sync status
if (isSyncing) {
if (!_rotationController.isAnimating) {