8000 fix(sync): stop infinite re-fetch loop during bidirectional pagination by ahmet-cetinkaya · Pull Request #293 · ahmet-cetinkaya/whph · GitHub
[go: up one dir, main page]

Skip to content

fix(sync): stop infinite re-fetch loop during bidirectional pagination - #293

Merged
ahmet-cetinkaya merged 4 commits into
mainfrom
fix/sync-infinite-loop-pagination
Jul 4, 2026
Merged

ahmet-cetinkaya merged 4 commits into
mainfrom
fix/sync-infinite-loop-pagination

Conversation

@ahmet-cetinkaya
Copy link
Copy Markdown
Owner

🚀 Motivation and Context

Bidirectional sync between mobile and desktop never completes. When syncing 10,000+ AppUsageTimeRecord records, the client re-requests ALL server pages on every outer-loop iteration, and the server resets its page cursor to -1 on exhaustion — restarting pagination from page 0. This produces an infinite re-fetch loop (~20,000 redundant page requests).

Root cause (verified via log analysis + code trace):

  • Client (sync_pagination_service.dart): requestAdditionalServerPages() called inside the per-iteration while loop, not once
  • Server (sync_incoming_handler.dart): setLastSentServerPage(..., -1) on exhaustion path restarts the auto-advance counter

⚙️ Implementation Details

Client (sync_pagination_service.dart):

  • Removed per-iteration requestAdditionalServerPages() call
  • Added tracking of lastReceivedServerPage and totalServerPages during lockstep
  • Added single post-loop requestAdditionalServerPages() call for remaining server pages

Server (sync_incoming_handler.dart):

  • Page cursor resets only when pageIndex == 0 (new session)
  • Removed exhaustion resets at lines 134-136 and 239 that caused the restart-from-0 behavior

Tests (sync_incoming_handler_test.dart):

  • 4 new tests: cursor resets at pageIndex=0, does not reset at pageIndex>0, does not reset on exhaustion, entity-type isolation

📋 Checklist for Reviewer

  • All 28 related sync tests pass (flutter test on commands + services)
  • flutter analyze — no issues
  • dart format — clean
  • Commit follows conventional commit format
  • Manual two-device sync verification recommended (live sync cannot be run in CI)

🔗 Related

Addresses the issue where sync from SAMSUNG SM-S911B to NixOS 26.11 server hangs indefinitely.

- Client: fetch remaining server pages once after local pages exhaust,
  instead of re-requesting on every outer-loop iteration
- Server: reset page cursor only at session start (pageIndex==0),
  removing mid-exhaustion resets that restarted from page 0
- Add SyncIncomingHandler tests verifying session-aware cursor reset
@ahmet-cetinkaya ahmet-cetinkaya self-assigned this Jul 4, 2026
@gemini-code-assist gemini-code-assist Bot left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the paginated sync process to prevent unbounded re-fetch loops. It moves the server-side page cursor reset to the start of a new sync session (when pageIndex is 0) and defers fetching remaining server pages until after local data is exhausted. Additionally, comprehensive unit tests are added to verify the cursor reset behavior. The review feedback highlights two key improvements: a high-severity issue where lockstep pages overwrite each other in storage, potentially causing data loss, and a medium-severity recommendation to guard the final sequential page fetch against cancelled sync operations.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

- Convert sync_pagination_service_test from testWidgets to plain test()
  to prevent Future.delayed hanging under fake async clock (root cause
  of 10+ minute test suite stalls and cascading 750+ failures)
- Fix sync_incoming_handler_test: use @GenerateNiceMocks instead of
  hand-rolled Mock classes to resolve null-safety dummy value issues
  with non-nullable Future<void> return types
- Fix date_filter_setting_test: compute expected week boundaries from
  DateTime constructor instead of assuming same month as now (failed
  when week start falls in previous month near month boundaries)
- Fix lockstep loop test to actually exercise 3-page sequence and
  verify cursor reset fires exactly once on pageIndex 0
- Extract helpers from syncEntityWithPagination into focused private
  methods for improved readability and testability
@ahmet-cetinkaya
ahmet-cetinkaya marked this pull request as ready for review July 4, 2026 18:50
…lation

- Use storeAdditionalServerPage for pages with known server page index
  to prevent silent data loss from key collisions in _pendingResponseData
- Add _isSyncCancelled guard in _fetchRemainingServerPages to skip
  unnecessary work and log noise after cancellation
@ahmet-cetinkaya
ahmet-cetinkaya merged commit b531154 into main Jul 4, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0