Skip to content

Fix cache initialization race condition for OAuth - #197

Merged
ddon merged 2 commits into
BeamLabEU:devfrom
timujinne:dev
Nov 25, 2025
Merged

Fix cache initialization race condition for OAuth#197
ddon merged 2 commits into
BeamLabEU:devfrom
timujinne:dev

Conversation

@timujinne

Copy link
Copy Markdown
Contributor

Summary

Fixed race condition in cache initialization that caused OAuth configuration to fail on cold starts due to Settings cache returning empty data.

Changes

Cache System

  • Remove critical_warmer parameter: Simplified cache initialization to load all data in single operation instead of loading critical settings first then remaining settings
  • Add retry mechanism: Implement exponential backoff retry logic for empty cache results (repository not ready yet)
  • Improve handle_continue: Load all settings data synchronously when sync_init is enabled, preventing overwrites from async operations

Files Modified

  • lib/phoenix_kit/cache/cache.ex - Updated warming strategy with retry logic
  • lib/phoenix_kit/supervisor.ex - Simplified cache configuration

Problem Solved

Issue: On cold start, cache would:

  1. Load critical OAuth settings successfully
  2. Then load full cache asynchronously
  3. Full cache returned empty map (repo not ready)
  4. Empty map overwrote critical settings
  5. OAuth configuration failed

Solution: Load all data once in handle_continue with retry logic instead of critical-then-full strategy.

Benefits

  • Single data load eliminates overwrite race conditions
  • Retry logic handles repository initialization delays
  • OAuth credentials load reliably on cold starts
  • Maintains non-blocking supervisor initialization
  • Simpler configuration (one warmer instead of two)

Test Plan

  • Code compiles without errors
  • Credo static analysis passes
  • Dialyzer type checking passes
  • Manual testing: Cold start with OAuth configuration
  • Verified cache loads 65+ settings entries on startup
  • Verified OAuth configuration loads successfully
  • Tested Google OAuth authentication flow

Related Commits

  • 864e194 - Fix cache initialization to load all data synchronously
  • 609189c - Fix UeberAuth configuration access and remove unused cache functions

Problems fixed:
1. PhoenixKit.Config.UeberAuth was incorrectly accessing Ueberauth
   configuration through PhoenixKit.Config methods, which only look
   in the :phoenix_kit application environment
2. Cache module had unused warming functions from old implementation

Changes:
- Fixed get_all() to use Application.get_env(:ueberauth, Ueberauth, [])
  instead of Config.get_list(:ueberauth, [])
- Fixed set_all() to use Application.put_env(:ueberauth, Ueberauth, config)
  instead of Config.set(:ueberauth, config)
- Removed unused handle_cache_warming/4 and handle_sync_warming/3 functions
  from PhoenixKit.Cache

This ensures UeberAuth configuration is correctly read from and written
to the :ueberauth application environment where it belongs, and removes
dead code from the Cache module.
Update cache warming strategy to load all settings data during
synchronous initialization instead of loading critical settings
first then remaining settings asynchronously.

Changes:
- Remove critical_warmer parameter from cache initialization
- Load all data in handle_continue when sync_init is enabled
- Add retry mechanism with exponential backoff for empty results
- Simplify supervisor configuration to use single warmer function

This prevents race conditions where critical OAuth settings are
loaded first but then overwritten by async full cache loading
returning empty results if repository is not yet ready.

Benefits:
- Single data load eliminates overwrite issues
- Retry logic handles repository initialization delays
- OAuth configuration loads reliably on cold starts
- Maintains non-blocking supervisor initialization
@ddon
ddon merged commit a8a2ac8 into BeamLabEU:dev Nov 25, 2025
6 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.

2 participants