Skip to content

Conversation

RyanLRKuhn
Copy link

@RyanLRKuhn RyanLRKuhn commented Oct 21, 2025

Summary

Fixes a race condition where concurrent fetch requests could result in an older response overwriting a newer one. This occurred when a later fetch request completed before an earlier one, causing stale data to be stored.

Changes

  • Added sequence numbering to fetch requests to track request order
  • Modified storeVariants() to only store responses from the most recent fetch
  • Added comprehensive test coverage demonstrating the race condition and validating the fix

Test Plan

  • Added new test file __tests__/race-condition.test.ts with two test cases:
    • Test demonstrating the race condition with delayed HTTP clients
    • Test verifying multiple rapid fetches always use the last initiated request
  • All existing tests continue to pass
  • TypeScript compilation passes
  • ESLint passes

Technical Details

The fix introduces two instance variables:

  • fetchSequenceNumber: Increments with each new fetch request
  • storedFetchSequenceNumber: Tracks the sequence number of the currently stored data

When storing variants, the method now checks if the incoming sequence number is newer than what's already stored. If not, the stale response is discarded with a debug log message.

This ensures that in scenarios where:

  1. Fetch A starts at t=0ms
  2. Fetch B starts at t=10ms
  3. Fetch B completes at t=60ms
  4. Fetch A completes at t=200ms

...the client will correctly store data from Fetch B and ignore the stale response from Fetch A.

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