Skip to content

Fix workout recovery race conditions and add validation for corrupted state#77

Merged
wulfland merged 3 commits into
fix-complete-workoutfrom
copilot/sub-pr-76
Feb 10, 2026
Merged

Fix workout recovery race conditions and add validation for corrupted state#77
wulfland merged 3 commits into
fix-complete-workoutfrom
copilot/sub-pr-76

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 10, 2026

Addresses four critical issues in workout session recovery and auto-save identified in PR #76 review.

Changes

Data Loss Prevention in endWorkout

  • Stop auto-save interval synchronously before any state mutation
  • Clear localStorage only after successful DB write (try-catch protected)
  • Preserves recovery state on DB errors
// Stop interval first to prevent race conditions
if (autoSaveTimerRef.current) {
  window.clearInterval(autoSaveTimerRef.current);
  autoSaveTimerRef.current = null;
}

try {
  await createWorkout(completedWorkout);
  clearAutoSavedWorkout();  // Only clear after success
} catch (error) {
  // Workout remains in localStorage for recovery
  throw error;
}

Auto-Save Closure Stale State Fix

  • Use refs (workoutRef, isActiveRef) to track current state
  • Auto-save callback reads from refs instead of closure-captured values
  • Eliminates window where stale data could be saved after workout ends

Validation for Corrupted Recovery State

  • Reject non-object JSON values (null, primitives, arrays)
  • Auto-clear corrupted data from localStorage
  • Type-safe casting only after validation
// Validate before accessing properties
if (!workout || typeof workout !== 'object' || Array.isArray(workout)) {
  localStorage.removeItem('activeWorkout');
  return null;
}

Test Coverage

  • 14 new unit tests covering edge cases: malformed JSON, type mismatches, completed workouts, Date revival
  • All localStorage error paths validated

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 10, 2026 09:05
Co-authored-by: wulfland <5276337+wulfland@users.noreply.github.com>
Co-authored-by: wulfland <5276337+wulfland@users.noreply.github.com>
Copilot AI changed the title [WIP] Enhance workout recovery and auto-save logic Fix workout recovery race conditions and add validation for corrupted state Feb 10, 2026
Copilot AI requested a review from wulfland February 10, 2026 09:08
@wulfland wulfland marked this pull request as ready for review February 10, 2026 09:09
@wulfland wulfland merged commit 4939981 into fix-complete-workout Feb 10, 2026
@wulfland wulfland deleted the copilot/sub-pr-76 branch February 10, 2026 09:09
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.

2 participants