Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Fix sync data loss when clobber guard refuses pull

Barnabasfb8faac33379
diff --git a/lib/services/progress_sync_service.dart b/lib/services/progress_sync_service.dart
index 55010067..73896327 100644
--- a/lib/services/progress_sync_service.dart
+++ b/lib/services/progress_sync_service.dart
@@ -72,7 +72,8 @@ class ProgressSyncService {
/// Cache server-known progress locally without marking it as a pending sync.
/// Skips items with a pending sync so unsynced local writes (e.g. a finished
/// state saved while offline) are not clobbered by a stale server snapshot.
- Future<void> cacheServerProgress({
+ /// Returns true if the cache was written, false if it was refused.
+ Future<bool> cacheServerProgress({
required String itemId,
required double currentTime,
required double duration,
@@ -81,7 +82,7 @@ class ProgressSyncService {
final pendingList = await ScopedPrefs.getStringList('pending_syncs');
if (pendingList.contains(itemId)) {
debugPrint('[Sync] Skipping server cache for $itemId - pending local sync would be clobbered (isFinished=$isFinished)');
- return;
+ return false;
}
final data = {
'itemId': itemId,
@@ -92,6 +93,7 @@ class ProgressSyncService {
'timestamp': 0,
};
await ScopedPrefs.setString('progress_$itemId', jsonEncode(data));
+ return true;
}
/// Get locally saved progress for an item.
@@ -241,15 +243,23 @@ class ProgressSyncService {
hasOfflineListening: hasOfflineListening,
)) {
debugPrint('[Sync] Server is newer for $itemId: server=$serverTime s ($serverTimestamp) vs local=$localTime s ($localTimestamp) — pulling');
- await cacheServerProgress(
+ final cached = await cacheServerProgress(
itemId: itemId,
currentTime: serverTime,
duration: localDuration,
isFinished: serverProgress['isFinished'] as bool? ?? false,
);
- final updated = await ScopedPrefs.getStringList('pending_syncs');
- updated.remove(itemId);
- await ScopedPrefs.setStringList('pending_syncs', updated);
+ if (cached) {
+ // Pull succeeded - safe to clear pending.
+ final updated = await ScopedPrefs.getStringList('pending_syncs');
+ updated.remove(itemId);
+ await ScopedPrefs.setStringList('pending_syncs', updated);
+ } else {
+ // Clobber guard refused. Keep pending so the next flush retries
+ // and eventually pushes local; otherwise the unsynced local
+ // entry sits in limbo and gets overwritten on next playback.
+ debugPrint('[Sync] Keeping $itemId in pending_syncs after pull was refused by clobber guard');
+ }
continue;
}
debugPrint('[Sync] Local is newer for $itemId: local=$localTime s ($localTimestamp) vs server=$serverTime s ($serverTimestamp) — pushing');
diff --git a/lib/services/sync_logic.dart b/lib/services/sync_logic.dart
index b509130e..77540c5b 100644
--- a/lib/services/sync_logic.dart
+++ b/lib/services/sync_logic.dart
@@ -27,6 +27,11 @@ class SyncLogic {
return raw > maxRetryDelay ? maxRetryDelay : raw;
}
+ /// Position-gap threshold (seconds) above which local always wins. A larger
+ /// gap can't be explained by save/sync race or clock drift - it indicates
+ /// real unsynced listening progress that must not be discarded.
+ static const double localAheadSafetySeconds = 30.0;
+
/// Decide whether to pull server progress or push local progress when
/// flushing a pending sync.
///
@@ -35,6 +40,10 @@ class SyncLogic {
///
/// Rules:
/// - If local timestamp is newer or equal, always push local.
+ /// - If local position is more than [localAheadSafetySeconds] ahead of
+ /// server, push local. A multi-minute gap is real listening progress,
+ /// not a save race - timestamps lie when another device touched the
+ /// server with its own (older) cached position.
/// - If server timestamp is newer:
/// - If server position is at or ahead of local position, pull server.
/// - Else if there is offline listening time accumulated locally,
@@ -48,6 +57,7 @@ class SyncLogic {
required bool hasOfflineListening,
}) {
if (serverTimestamp <= localTimestamp) return false;
+ if (localTime - serverTime > localAheadSafetySeconds) return false;
return serverTime >= localTime || !hasOfflineListening;
}
}