fix: concurrency issue (#21830)

This commit is contained in:
Alex 2025-09-11 14:02:03 -05:00 committed by GitHub
parent 722a464e23
commit 42a03f2556
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 90 additions and 73 deletions

View file

@ -169,7 +169,10 @@ class BackgroundWorkerBgService extends BackgroundWorkerFlutterApi {
}
try {
final backgroundSyncManager = _ref.read(backgroundSyncProvider);
_isCleanedUp = true;
_ref.dispose();
_cancellationToken.cancel();
_logger.info("Cleaning up background worker");
final cleanupFutures = [
@ -179,14 +182,13 @@ class BackgroundWorkerBgService extends BackgroundWorkerFlutterApi {
}),
_drift.close(),
_driftLogger.close(),
_ref.read(backgroundSyncProvider).cancel(),
_ref.read(backgroundSyncProvider).cancelLocal(),
backgroundSyncManager.cancel(),
backgroundSyncManager.cancelLocal(),
];
if (_isar.isOpen) {
cleanupFutures.add(_isar.close());
}
_ref.dispose();
await Future.wait(cleanupFutures);
_logger.info("Background worker resources cleaned up");
} catch (error, stack) {
@ -195,6 +197,8 @@ class BackgroundWorkerBgService extends BackgroundWorkerFlutterApi {
}
Future<void> _handleBackup() async {
await runZonedGuarded(
() async {
if (!_isBackupEnabled || _isCleanedUp) {
_logger.info("[_handleBackup 1] Backup is disabled. Skipping backup routine");
return;
@ -224,6 +228,11 @@ class BackgroundWorkerBgService extends BackgroundWorkerFlutterApi {
return _ref
.read(uploadServiceProvider)
.startBackupWithHttpClient(currentUser.id, networkCapabilities.hasWifi, _cancellationToken);
},
(error, stack) {
debugPrint("Error in backup zone $error, $stack");
},
);
}
Future<void> _syncAssets({Duration? hashTimeout}) async {

View file

@ -1,9 +1,10 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/domain/models/store.model.dart';
import 'package:immich_mobile/domain/services/sync_linked_album.service.dart';
import 'package:immich_mobile/providers/user.provider.dart';
import 'package:immich_mobile/entities/store.entity.dart';
Future<void> syncLinkedAlbumsIsolated(ProviderContainer ref) {
final user = ref.read(currentUserProvider);
final user = Store.tryGet(StoreKey.currentUser);
if (user == null) {
return Future.value();
}

View file

@ -46,7 +46,7 @@ void main() async {
await Bootstrap.initDomain(isar, drift, logDb);
await initApp();
// Warm-up isolate pool for worker manager
await workerManager.init(dynamicSpawning: false);
await workerManager.init(dynamicSpawning: true);
await migrateDatabaseIfNeeded(isar, drift);
HttpSSLOptions.apply();

View file

@ -31,6 +31,8 @@ Cancelable<T?> runInIsolateGentle<T>({
}
return workerManager.executeGentle((cancelledChecker) async {
await runZonedGuarded(
() async {
BackgroundIsolateBinaryMessenger.ensureInitialized(token);
DartPluginRegistrant.ensureInitialized();
@ -57,21 +59,20 @@ Cancelable<T?> runInIsolateGentle<T>({
log.severe("Error in runInIsolateGentle ${debugLabel == null ? '' : ' for $debugLabel'}", error, stack);
} finally {
try {
ref.dispose();
await LogService.I.dispose();
await logDb.close();
await ref.read(driftProvider).close();
await drift.close();
// Close Isar safely
try {
final isar = ref.read(isarProvider);
if (isar.isOpen) {
await isar.close();
}
} catch (e) {
debugPrint("Error closing Isar: $e");
}
ref.dispose();
} catch (error, stack) {
debugPrint("Error closing resources in isolate: $error, $stack");
} finally {
@ -81,5 +82,11 @@ Cancelable<T?> runInIsolateGentle<T>({
}
}
return null;
},
(error, stack) {
debugPrint("Error in isolate zone: $error, $stack");
},
);
return null;
});
}