From 3cd2d7f657ea12e7d4cf2bd2aa289cdcdd836ce2 Mon Sep 17 00:00:00 2001 From: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com> Date: Tue, 20 Jan 2026 21:28:17 +0530 Subject: [PATCH] fix: remote sync errors in backup page --- .../providers/background_sync.provider.dart | 8 +++- mobile/lib/utils/isolate.dart | 46 +++++++++++-------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/mobile/lib/providers/background_sync.provider.dart b/mobile/lib/providers/background_sync.provider.dart index 37b3145eb4..849d86f301 100644 --- a/mobile/lib/providers/background_sync.provider.dart +++ b/mobile/lib/providers/background_sync.provider.dart @@ -21,7 +21,13 @@ final backgroundSyncProvider = Provider((ref) { backupProvider.updateError(isSuccess == true ? BackupError.none : BackupError.syncFailed); } }, - onRemoteSyncError: syncStatusNotifier.errorRemoteSync, + onRemoteSyncError: (error) { + syncStatusNotifier.errorRemoteSync(error); + final backupProvider = ref.read(driftBackupProvider.notifier); + if (backupProvider.mounted) { + backupProvider.updateError(BackupError.syncFailed); + } + }, onLocalSyncStart: syncStatusNotifier.startLocalSync, onLocalSyncComplete: syncStatusNotifier.completeLocalSync, onLocalSyncError: syncStatusNotifier.errorLocalSync, diff --git a/mobile/lib/utils/isolate.dart b/mobile/lib/utils/isolate.dart index 143b9ea55f..49f7afe44d 100644 --- a/mobile/lib/utils/isolate.dart +++ b/mobile/lib/utils/isolate.dart @@ -172,6 +172,9 @@ class _IsolateTaskRunner { void _cleanup() { if (_isCleanedUp) return; + if (!_completer.isCompleted) { + _completer.completeError(Exception("Isolate task cleaned up without completing.")); + } _isCleanedUp = true; _cleanupTimeoutTimer?.cancel(); @@ -249,9 +252,9 @@ Future _isolateEntryPoint(_IsolateTaskConfig config) async { }); final log = Logger("IsolateWorker[${config.debugLabel}]"); - try { - await runZonedGuarded( - () async { + await runZonedGuarded( + () async { + try { ref = ProviderContainer( overrides: [ dbProvider.overrideWithValue(isar), @@ -269,27 +272,30 @@ Future _isolateEntryPoint(_IsolateTaskConfig config) async { } else { log.fine("Task completed but was cancelled - not sending result"); } - }, - (error, stack) { - log.severe("Uncaught error in isolate zone", error, stack); + } catch (error, stack) { + log.severe("Error in isolate execution", error, stack); config.mainSendPort.send(_ErrorMessage(error, stack)); - }, - ); - } catch (error, stack) { - log.severe("Error in isolate execution", error, stack); - config.mainSendPort.send(_ErrorMessage(error, stack)); - } finally { - try { + } finally { + try { + receivePort.close(); + unawaited(subscription.cancel()); + await _cleanupResources(ref, isar, drift, logDb); + } catch (error, stack) { + dPrint(() => "Error during isolate cleanup: $error with stack: $stack"); + } finally { + unawaited(subscription.cancel()); + config.mainSendPort.send(const _DoneMessage()); + } + } + }, + (error, stack) async { + dPrint(() => "Uncaught error in isolate zone: $error, $stack"); receivePort.close(); unawaited(subscription.cancel()); await _cleanupResources(ref, isar, drift, logDb); - } catch (error, stack) { - dPrint(() => "Error during isolate cleanup: $error with stack: $stack"); - } finally { - unawaited(subscription.cancel()); - config.mainSendPort.send(const _DoneMessage()); - } - } + config.mainSendPort.send(_ErrorMessage(error, stack)); + }, + ); } CancellableTask runInIsolateGentle({