Fix race condition causing syncing loaders to get stuck #2412

Merged
zachgoll merged 1 commits from zachgoll/maybe-881-occasionally-syncing-loaders-get-stuck-because-sync-status into main 2025-06-26 03:23:15 +08:00
zachgoll commented 2025-06-26 03:16:15 +08:00 (Migrated from github.com)

Problem

The sync status monitor was experiencing a race condition that caused syncing loaders to appear stuck even after syncs completed.

Root Cause

The issue occurred because:

  1. When a sync changed state, handle_transition would update family.latest_sync_activity_at
  2. This timestamp is used in the cache key for SyncStatusMonitor
  3. However, the timestamp update happened inside the database transaction
  4. Other connections could read the old timestamp value before the transaction committed
  5. This caused them to use a stale cache key and retrieve outdated sync status

Solution

Move the timestamp update to an after_commit callback. This ensures:

  • The sync state change is fully committed and visible to all database connections
  • The timestamp is updated only after the commit
  • Any subsequent cache reads will see the new timestamp and invalidate properly

This eliminates the race condition while maintaining all existing functionality that depends on these timestamps for cache invalidation.

## Problem The sync status monitor was experiencing a race condition that caused syncing loaders to appear stuck even after syncs completed. ## Root Cause The issue occurred because: 1. When a sync changed state, `handle_transition` would update `family.latest_sync_activity_at` 2. This timestamp is used in the cache key for `SyncStatusMonitor` 3. However, the timestamp update happened inside the database transaction 4. Other connections could read the old timestamp value before the transaction committed 5. This caused them to use a stale cache key and retrieve outdated sync status ## Solution Move the timestamp update to an `after_commit` callback. This ensures: - The sync state change is fully committed and visible to all database connections - The timestamp is updated only after the commit - Any subsequent cache reads will see the new timestamp and invalidate properly This eliminates the race condition while maintaining all existing functionality that depends on these timestamps for cache invalidation.
Sign in to join this conversation.