Skip to content

fix(provider): discard stale drinks responses on rapid festival switch - #263

Merged
richardthe3rd merged 2 commits into
mainfrom
claude/analyze-repo-weaknesses-5nBgy
May 16, 2026
Merged

fix(provider): discard stale drinks responses on rapid festival switch#263
richardthe3rd merged 2 commits into
mainfrom
claude/analyze-repo-weaknesses-5nBgy

Conversation

@richardthe3rd

Copy link
Copy Markdown
Owner

Rapid festival switches could result in the slower first request
overwriting the faster second request's results, showing the wrong
festival's drinks. Fix by incrementing a load token on every new
drinks load (setFestival, loadDrinks) and discarding any response
whose token no longer matches the current one.

Adds a test that verifies the last-selected festival always wins when
two setFestival calls race and the first is slower.

https://claude.ai/code/session_01P4MUavTmHYCgaCTKVhEFf4

Copilot AI review requested due to automatic review settings May 13, 2026 21:05

Copilot AI 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.

Pull request overview

Fixes a race condition where a slower in-flight drinks fetch could overwrite a newer festival's drinks. Introduces a monotonically incremented _drinksLoadToken captured at the start of each loadDrinks / setFestival call; responses (success and error paths) are discarded when the token no longer matches the latest one, and the trailing _isLoading = false / notifyListeners() is also gated on token equality. Adds a test that races two setFestival calls.

Changes:

  • Add _drinksLoadToken counter incremented on every new load.
  • Gate success, catch, and finally blocks in loadDrinks and _loadDrinksInternal on token equality; thread token through _loadDrinksInternal(int token).
  • Add a race-condition test for rapid festival switching.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lib/providers/beer_provider.dart Adds load-token mechanism to discard stale drink responses on rapid festival switches.
test/beer_provider_test.dart Adds a test intended to verify that the last-selected festival's drinks win when two setFestival calls race.

Comment on lines +1467 to +1484
if (festival.id == 'cbf2024') {
await Future.delayed(const Duration(milliseconds: 100));
return [drinkA];
}
return [drinkB];
});

await provider.initialize();

// Switch to A (slow), then immediately to B (fast) — don't await A
final futureA = provider.setFestival(festivalA);
final futureB = provider.setFestival(festivalB);
await Future.wait([futureA, futureB]);

// B's result must win; A's stale response must be discarded
expect(provider.currentFestival.id, 'cbf2025');
expect(provider.drinks.length, 1);
expect(provider.drinks.first.name, 'Ale B');
Comment thread lib/providers/beer_provider.dart Outdated
Comment on lines +341 to +346
Future<void> _loadDrinksInternal(int token) async {
try {
// Repository returns drinks with favorites and ratings already populated
_allDrinks = await _drinkRepository!.getDrinks(currentFestival);
final drinks = await _drinkRepository!.getDrinks(currentFestival);
if (token != _drinksLoadToken) return;
_allDrinks = drinks;
@github-actions

github-actions Bot commented May 13, 2026

Copy link
Copy Markdown
Contributor

LCOV of commit 1ba1593 during CI #182

Summary coverage rate:
  lines......: 77.2% (2449 of 3173 lines)
  functions..: no data found
  branches...: no data found

Files changed coverage rate:
                                                      |Lines       |Functions  |Branches    
  Filename                                            |Rate     Num|Rate    Num|Rate     Num
  ==========================================================================================
  lib/providers/beer_provider.dart                    | 0.0%    269|    -     0|    -      0

@codecov

codecov Bot commented May 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Cloudflare Pages Preview

Your preview deployment is ready!

Preview URL: https://claude-analyze-repo-weakness.staging-cambeerfestival.pages.dev

This preview will be automatically updated when you push new commits to this PR.

claude added 2 commits May 16, 2026 07:50
Rapid festival switches could result in the slower first request
overwriting the faster second request's results, showing the wrong
festival's drinks. Fix by incrementing a load token on every new
drinks load (setFestival, loadDrinks) and discarding any response
whose token no longer matches the current one.

Adds a test that verifies the last-selected festival always wins when
two setFestival calls race and the first is slower.

https://claude.ai/code/session_01P4MUavTmHYCgaCTKVhEFf4
Two issues identified in Copilot review of #263:

1. _loadDrinksInternal now accepts an explicit Festival parameter instead
   of reading the live currentFestival getter. After the awaits in
   setFestival (analytics, persist), a concurrent setFestival call could
   overwrite _currentFestival before getDrinks ran, causing both loads to
   fetch for the same festival and making the mock's per-festival delay
   logic ineffective.

2. Race condition test was a no-op: initialize() set _currentFestival to
   festivalA (cbf2024, the defaultFestivalId), so setFestival(festivalA)
   hit the early-return guard and never ran. Added festivalC (cbf2023) as
   the default so both race competitors actually initiate a getDrinks call.
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Cloudflare Pages Preview

Your preview deployment is ready!

Preview URL: https://claude-analyze-repo-weakness.staging-cambeerfestival.pages.dev

This preview will be automatically updated when you push new commits to this PR.

@richardthe3rd
richardthe3rd merged commit 30f4372 into main May 16, 2026
10 checks passed
@richardthe3rd
richardthe3rd deleted the claude/analyze-repo-weaknesses-5nBgy branch May 16, 2026 08:07
This was referenced May 16, 2026
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.

3 participants