Skip to content
Source
Sign inSign up
TanStack/query

useQuery stays pending in headless Chromium despite queryFn resolving successfully #10509

Send to my agent
ClosedML-o13(GitHub profile, opens in a new tab) opened this issue 3 comments

Describe the bug

useQuery stays in status: "pending" / fetchStatus: "fetching" indefinitely inside headless Chromium (Puppeteer), even though every underlying HTTP call in the queryFn completes with 200 and an inline replica of the exact same fetch sequence resolves successfully in under a second.

The queryFn performs 5 sequential supabase.from().select() calls, a synchronous .map() build phase, and returns. Puppeteer's network listeners confirm all 5 requests fire and respond with 200 OK. An async IIFE running the identical sequence in a parallel useEffect logs all sequential awaits completed after ~955 ms total — but the useQuery-wrapped version never transitions from pending to success. No error is thrown. No state change occurs until the React Query's own retry kicks in ~24 s later. After max retries, the query is still stuck pending (not error).

Expected: after queryFn resolves, the hook's status transitions to "success" and data is populated.

Your minimal, reproducible example

Unable to produce a CodeSandbox — the bug only reproduces in headless Chromium via Puppeteer. The same code runs normally in a regular Chrome / Firefox browser window.

Steps to reproduce

  1. Vite + React 18 + @tanstack/react-query v5.83.0 app with a useQuery hook whose queryFn does multiple sequential supabase-js fetches
  2. Render the app via puppeteer (page.goto(url, { waitUntil: 'domcontentloaded' }))
  3. Inside the page, attach useEffect listeners that log status, fetchStatus, navigator.onLine every 3 s
  4. Observe status stays "pending" indefinitely

Confirmed by attaching puppeteer page.on('request') / page.on('response') listeners: every fetch completes with 200, an inline replica of the same fetches in a parallel useEffect logs each step's completion time, but the React Query state never advances.

Expected behavior

After every await in the queryFn resolves and the function returns, the query's status should become "success" and subscribers should re-render with data.

How often does this bug happen?

Every time

Screenshots or Videos

No screenshots. Text log excerpt:

[req] GET .../shoe_models?... [res 200] .../shoe_models?... [req] GET .../model_views?... [res 200] .../model_views?... (...3 more endpoints, all 200...) [trace] s1 shoe_models DONE 275ms rows=1 err=null [trace] s2 model_views DONE 180ms [trace] s3 model_zones DONE 150ms [trace] s4 view_zone_masks DONE 141ms rows=43 [trace] s5 zone_materials DONE 209ms rows=27 [trace] all sequential awaits completed [render-poll] status:"pending" fetchStatus:"fetching" views:0 [render-poll] status:"pending" fetchStatus:"fetching" views:0 (repeats every 3s for 25s) [req] GET .../shoe_models?... ← React Query retry, same result

Platform

  • Browser: Chromium 120 (system install in node:20-slim), driven by puppeteer 23.0.0
  • OS: Debian 12 (bookworm) inside Docker container
  • Same code works in user-agent-matched Chrome 120 on Windows 11 (browser mode)
  • networkMode: 'always' set globally on the QueryClient (does not help)
  • navigator.onLine === true

Tanstack Query adapter

react-query

TanStack Query version

5.83.0

TypeScript version

5.8.3

Additional context

What we ruled out:

  • Offline-detection pause — networkMode: 'always' does not fix
  • Cloudflare / UA detection — overriding page.setUserAgent to a normal Chrome UA does not fix
  • Network / DNS — container-side curl / fetch resolve in ~100 ms
  • supabase-js promise hanging — inline supabase.from().select() outside React Query resolves normally
  • Event loop blocked — setInterval keeps firing throughout
  • React not re-rendering — interval-based log confirms re-renders continue
  • Concurrent-mode suspension — no boundaries involved

Workaround: replaced useQuery with useState + useEffect running the exact same fetches. Page renders in ~1 s instead of eternal hang.

  1. In a React Native application, after a certain number of sequential API requests, useQuery occasionally gets stuck in a perpetual pending state. The UI loader spins indefinitely, and the query state logs as follows:

    { status: 'pending', fetchStatus: 'fetching', isPending: true, isSuccess: false, isError: false }

    However, checking the network logs confirms that the RESTful API successfully returned a 200 OK response with the expected JSON payload. The network layer finishes the job, but TanStack Query never transitions to the success state. Crucial Observation: When the app is moved to the background and then brought back to the foreground, the state immediately recovers and updates correctly to:

    { status: 'success', fetchStatus: 'idle', isPending: false, isSuccess: true }

    What I've Already Checked / Tried:

    1. Not a Return/Promise issue: The exact same API endpoints and queryFn implementations work perfectly fine under normal usage. This issue only triggers after making a stream of multiple requests over a short period.
    2. Not a Circular Dependency issue: I explicitly disabled structural sharing globally, but the issue still persists:
    export const queryClient = new QueryClient({
      defaultOptions: { queries: { structuralSharing: false } },
    });
    1. Not an enabled flag issue: The enabled flag is true. The fetchStatus is actively 'fetching', not 'idle'. 4. Not an Offline/Network issue: The device has a stable internet connection, and native network logs verify the successful HTTP handshake and data transfer. 5. UI Responsiveness: The UI itself does not freeze; animations keep playing and navigation works, meaning the JS thread is not completely dead, but rather the specific state dispatch from TanStack Query seems to be dropped or batched indefinitely until a foreground event flushes the queue. Expected Behavior: Once the underlying Promise resolves with the API response, useQuery should transition to the success state and trigger a re-render without requiring an AppState change (background/foreground trigger). Environment: • "react-native": "0.85.3", • "@tanstack/react-query": "^5.100.14", • "axios": "^1.16.1",
  2. I found a mechanism that reproduces your exact symptom and a workaround you can test. I can't fully account for your headless-Chromium case, though, so here's what I'd want to pin down.

    When a query settles, query-core updates the query's state and the observer's result to success synchronously. But the notification that tells React's useSyncExternalStore to re-read its snapshot is dispatched separately, through notifyManager, which defaults to setTimeout(cb, 0) (a macrotask). So the new result already exists; it's the re-render that would surface it that sits on a macrotask. If that macrotask never runs, the hook keeps returning its last pending snapshot.

    That's what a backgrounded React Native app does: it pauses the timer queue, and foregrounding flushes it. It lines up with @OkancanCosar's "recovers the moment I background then foreground" report.

    You can test whether your case is this path:

    import { notifyManager } from '@tanstack/query-core'
    notifyManager.setScheduler(queueMicrotask)

    Microtasks run off the timer queue, so they sidestep timer throttling (a fully suspended JS runtime would still pause them). In a local repro with setTimeout(0) frozen, the query reached success internally while the subscriber never fired; switching to queueMicrotask delivered success with timers still frozen. Treat it as a diagnostic probe, not a blanket fix.

    Your headless-Chromium case doesn't quite fit a pure timer-pause. You mention a retry around 24s, which suggests timers do eventually run. What do status and fetchStatus show at that point, and does setScheduler(queueMicrotask) change anything? That would tell us whether it's the same notification path or something Puppeteer-specific.

    If it is the notification path, would you want query-core to make delivery resilient to paused or throttled timers by default, or is a custom scheduler the intended escape hatch for these environments?

  3. we will switch to queueMicroTask as a default in the next major version

  4. TkDodo closed this

  5. TkDodo added labelshas workaround

Sign in to join this conversation.