Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: Resolve race condition in AbortController handling during tab visibility changes #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
yushihang wants to merge 2 commits into Azure:main
base: main
Choose a base branch
Loading
from yushihang:fix/abort-controller-race-condition

Conversation

Copy link

@yushihang yushihang commented Mar 11, 2025
edited
Loading

Issue

close #95
A race condition exists in the error handling of aborted requests when rapidly switching browser tabs. This causes unnecessary request retries and incorrect error handling behavior.

Current Behavior

When switching browser tabs quickly:

  1. Request A is initiated with AbortController instance A
  2. Tab becomes hidden -> Controller A's abort() is called
  3. Tab becomes visible -> Request B starts with Controller B
  4. Request A's error handler executes but incorrectly checks Controller B's status
  5. This leads to unnecessary retries of intentionally aborted requests

Root Cause

The issue stems from using a shared global curRequestController variable across different async contexts. When error handling occurs asynchronously, it may check the abort status of a different controller instance than the one that initiated the request.

Solution

Store the AbortController instance in the function scope to ensure each request's error handling uses its own controller:

- curRequestController = new AbortController();
+ const currentController = new AbortController();
+ curRequestController = currentController;
 // Later in catch block:
- if (!curRequestController.signal.aborted)
+ if (!currentController.signal.aborted)

Impact

  • Eliminates unnecessary request retries
  • Correctly handles tab visibility changes
  • Improves error handling accuracy
  • No breaking changes to public API

Testing

Verified by:

  • Rapidly switching browser tabs
  • Confirming aborted requests don't trigger retries
  • Ensuring normal retry behavior for actual errors remains unchanged

yushihang and others added 2 commits March 11, 2025 20:42
Timeline of the race condition:
Time 0:
- AbortController instance A is created
- Request A is initiated with instance A
Time 1: (User switches tab, making it invisible)
- onVisibilityChange() is triggered
- abort() is called on instance A
- create() is queued to execute when tab becomes visible
Time 2: (User switches back, making tab visible) 
- create() executes
- AbortController instance B is created
- Request B is initiated with instance B
Time 3:
- Request A's abort error is caught in catch block
- Bug: Code checks instance B's abort status instead of instance A's
- Since instance B is not aborted, onerror callback is wrongly triggered
- This leads to an unnecessary retry attempt
Fix:
Store AbortController instance in local scope to ensure error handling uses the correct instance:
Before:
```ts
curRequestController = new AbortController();
// Later in catch block
if (!curRequestController.signal.aborted)
```
After:
```ts
const currentController = new AbortController(); 
curRequestController = currentController;
// Later in catch block
if (!currentController.signal.aborted)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Race Condition: Incorrect AbortController Status Check During Tab Visibility Changes

AltStyle によって変換されたページ (->オリジナル) /