Skip to content
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

Fix AbortError being triggered incorrectly on createApi endpoint timeout #4628

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

andrejpavlovic
Copy link
Contributor

This PR:

Copy link

codesandbox bot commented Sep 19, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link

netlify bot commented Sep 19, 2024

Deploy Preview for redux-starter-kit-docs ready!

Name Link
🔨 Latest commit d9a9106
🔍 Latest deploy log https://app.netlify.com/sites/redux-starter-kit-docs/deploys/66eb952228af7b0009923d8d
😎 Deploy Preview https://deploy-preview-4628--redux-starter-kit-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

codesandbox-ci bot commented Sep 19, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit d9a9106:

Sandbox Source
@examples-query-react/basic Configuration
@examples-query-react/advanced Configuration
@examples-action-listener/counter Configuration
rtk-esm-cra Configuration

…OUT_ERROR` instead of generic `AbortError`.
@andrejpavlovic andrejpavlovic force-pushed the fix/timeout-on-createApi-endpoint-should-throw-TIMEOUT_ERROR branch from b6c5d1f to d9a9106 Compare September 19, 2024 03:06
@phryneas
Copy link
Member

Could you please explain the reasoning here a bit? It's not immediately obvious to me.

@andrejpavlovic
Copy link
Contributor Author

Could you please explain the reasoning here a bit? It's not immediately obvious to me.

Sure thing! As I alluded to in #2915, the issue exists because of a bit of a race condition happening.

  1. When fetchBaseQuery times out, it triggers api.abort() on global abort controller (ie. on the one that is created inside createAsyncTrunk)
    setTimeout(() => {
    timedOut = true
    api.abort()
    }, timeout)
  2. That in turns rejects the abortPromise in createAsyncTrunk with the generic AbortError.
    const abortedPromise = new Promise<never>((_, reject) => {
    abortHandler = () => {
    reject({
    name: 'AbortError',
    message: abortReason || 'Aborted',
    })
    }
  3. Which then rejects the Promise.race promise here:
    finalAction = await Promise.race([
    abortedPromise,
    Promise.resolve(
    payloadCreator(arg, {
  4. And this rejection (the generic AbortError) gets returned by the createAsyncTrunk here:
    finalAction =
    err instanceof RejectWithValue
    ? rejected(null, requestId, arg, err.payload, err.meta)
    : rejected(err as any, requestId, arg)

So even though fetchBaseQuery would resolve to TIMEOUT_ERROR (see below), it wouldn't matter, because the global abortPromise would always resolve first.

} catch (e) {
return {
error: {
status: timedOut ? 'TIMEOUT_ERROR' : 'FETCH_ERROR',
error: String(e),

Since fetchBaseQuery is quite capable of resolving the timeout locally, there is no need to trigger the global abort inside of it.

The solution I applied was to create an AbortController inside of fetchBaseQuery that would handle the timeout locally, but would also abort if abort is triggered on the global signal:

let abortController: AbortController | undefined, signal = api.signal
if (timeout) {
abortController = new AbortController()
api.signal.addEventListener('abort', abortController.abort)
signal = abortController.signal
}

To sum it up, in this PR, we avoid the race condition in createAsyncThunk between 1. triggering global abort and 2. handling the timeout in fetchBaseQuery, by never triggering the global abort on timeout in the first place.

Of note: As far as I understand best practices for using AbortController, abort callback should not be passed to fetchBaseQuery in the first place, just the signal. I would go as far as suggest to remove passing this parameter:

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.

Timeout not throwing error with status TIMEOUT_ERROR
2 participants