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

Infinite query page param resets back to the first page during refetch if a page request is retried #8046

Closed
parkyoung555 opened this issue Sep 12, 2024 · 0 comments · Fixed by #8049

Comments

@parkyoung555
Copy link

parkyoung555 commented Sep 12, 2024

Describe the bug

I am using useInfiniteQuery to additively load more data into a table from a nextToken based list API (e.g. example AWS SDK list API). Requesting next pages and refetching works great for the most part, but I'm having some trouble figuring out a way to preserve the next page param for a retried request triggered by a manual or automated refetch.

When fetching next pages with fetchNextPage and the queryFn rejects with an error response, the request is retried as expected using the same page params. However, I noticed retried requests from a manual or automated refetch will start over back to the first page after the first error response is encountered. I understand the need to fetch sequentially from the first page for the refetch, however, I would expect the failed request to retry using the same page params and continue onto the next page request once a retry succeeds.

The backend service I'm working with has a reasonable rate limit set (50 TPM in a 1 minute sliding window), but I'm trying to ensure a manual or automatic refetch will be able to recover from worst-case rate limiting scenarios where the user can continuously rate-limit themselves by refetching a table view loaded with 50+ pages.

Original discussion: #8044

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/mqvdz4

Steps to reproduce

  1. Open code sandbox and open browser console to view logs
  2. Click the "Get next page" button 3 times to paginate through all the mock data. The second requested page is hard coded to simulate a rate limiting error and will be retried 3 times before succeeding.
  3. After loading all the data, click the "Refetch" button to invoke query refetch.
  4. You should be able to observe the page param being reset back to 0 during the refetch when the rate limiting error is encountered with page 2. This will continue to loop until the number of retry attempts are exhausted.

Example console logs:

// fetch next page

getting page 0...
got page 0
getting page 1...
got page 1
getting page 2... // simulating rate limit for page index 2
rate limited!
getting page 2...
rate limited!
getting page 2...
rate limited!
getting page 2...
got page 2 // rate limit quota restored
getting page 3...
got page 3

// refetch

getting page 0...
got page 0
getting page 1...
got page 1
getting page 2...
rate limited!
getting page 0... // page param is reset to 0, expected page 2 to be retried here
got page 0
getting page 1...
got page 1
getting page 2...
rate limited!
getting page 0... // repeatedly getting rate limited
got page 0
getting page 1...
got page 1
getting page 2...
rate limited!
getting page 0...
got page 0
getting page 1...
got page 1
getting page 2...
rate limited!
...

Expected behavior

During a manual and automated infinite query refetch, I expect a failed page request to retry using the same page params and continue onto the next page request once the retry succeeds. Currently, infinite query refetch will start over back to the first page for retry attempts after the first error response is encountered.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: macOS Sonoma 14.6.1
  • Browser: Chrome
  • Version: 128.0.6613.120 (Official Build) (x86_64)

Tanstack Query adapter

react-query

TanStack Query version

v5.51.23

TypeScript version

v5.2.2

Additional context

No response

TkDodo added a commit that referenced this issue Sep 12, 2024
* Add reproduction test for infinite loop retries (issue #8046)

* ci: apply automated fixes

* fix: retry for infinite queries

The retryer lives above the fetchFn, and it re-runs the fetchFn whenever a retry happens. Usually, the fetchFn is a thin wrapper around the actual queryFn passed by the user. However, for infinite queries, it fetches all pages in a loop.
The retryer breaks out of this loop if an error occurs on e.g. the second page, and then retries by running the fetchFn - which will re-set the loop

This fix hoists the currentPage counter out of the fetchFn - into the closure created by onFetch. The outer closure is created from running `query.fetch` once, so it won't be re-set between retries.
The fix also re-writes the fetch loop to always take the `currentPage` into account, where it was previously treating the first page differently

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Dominik Dorfmeister <[email protected]>
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 a pull request may close this issue.

1 participant