Azure / Azure/fetch-event-source

Race Condition: Incorrect AbortController Status Check During Tab Visibility Changes

Open
#95 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
2.9k
Forks
196
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.