fetch-event-source icon indicating copy to clipboard operation
fetch-event-source copied to clipboard

Race Condition: Incorrect AbortController Status Check During Tab Visibility Changes

Open yushihang opened this issue 8 months ago • 0 comments
trafficstars

Title: Race Condition: Incorrect AbortController Status Check During Tab Visibility Changes

Description

Bug Report

When rapidly switching browser tabs with openWhenHidden: false, the error handling logic incorrectly checks the abort status of a new request instead of the aborted one, leading to unnecessary retries.

Steps to Reproduce

  1. Create an EventSource connection with openWhenHidden: false
  2. Quickly switch to another tab and back
  3. Observe the network requests in DevTools

Current Behavior

  1. Initial request (A) starts with AbortController instance A
  2. Tab becomes hidden -> Controller A's abort() is called
  3. Tab becomes visible -> New request (B) starts with Controller B
  4. Request A's error handler executes but checks Controller B's status
  5. Since Controller B is not aborted, it triggers unnecessary retry

Expected Behavior

The error handler should check the abort status of the controller that initiated the request, not the current global controller instance.

Technical Details

The issue occurs because:

  • curRequestController is shared across async contexts
  • Error handling happens asynchronously after the controller reference has changed
  • This leads to checking the wrong controller's abort status

Impact

  • Unnecessary network requests
  • Incorrect error handling
  • Potential performance issues with rapid tab switching

Environment

  • Browser: All major browsers
  • Package Version: v2.0.1 (Latest version)
  • OS: All platforms

Suggested Fix

Store the AbortController instance in function scope to ensure error handling uses the correct instance for status checking.

Pull Request

https://github.com/Azure/fetch-event-source/pull/94

yushihang avatar Mar 11 '25 13:03 yushihang