Skip to content
Source
Sign inSign up
TanStack/query

Explicit gcTime schedules a GC timer during SSR, retaining the whole render in memory #12025

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

Describe the bug

On the server, a query with an explicit finite gcTime (a regular client-side option) schedules a real setTimeout during SSR. The server default of Infinity is meant to prevent this, but an explicit gcTime bypasses it.

In Node, a pending timer keeps the render's async context (AsyncLocalStorage) alive until it fires. As a result, the entire render stays in memory: the HTML, the RSC payload, and the Request held by react-dom/server.

The timer serves no purpose. The request's QueryClient is discarded together with the response, so there is nothing left to garbage-collect.

Our case: Next.js 16, with a single useQuery({ gcTime: 24h }) in the root provider. Every render left behind a 24-hour timer and about 1.4 MB of retained memory. A heap snapshot of an idle replica showed 272 Request objects and 268 pending timers. Replicas grew until they hit the memory limit and were OOM-killed.

Your minimal, reproducible example

https://gist.github.com/AdzerKI/44081bf1d74a64d43ce651018ce2cba9

Steps to reproduce

  1. Save gc-timer-retains-async-context.mjs from the gist
  2. npm i @tanstack/query-core
  3. node --expose-gc gc-timer-retains-async-context.mjs default
  4. node --expose-gc gc-timer-retains-async-context.mjs finite
  5. Compare pendingTimers and rss in the output

Expected behavior

On the server, no garbage-collection timer should be scheduled for any gcTime value, the same as with the server default.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

OS: Linux Runtime: Node.js 24.21.0 (server-side rendering, no browser involved)

Tanstack Query adapter

react-query

TanStack Query version

v5.104.2

TypeScript version

No response

Additional context

gcTime=default renders=2000 pendingTimers=0 external=2MB rss=119MB gcTime=60000 renders=2000 pendingTimers=2000 external=1002MB rss=1069MB

  1. @mgarcialeniolabs thanks, but there's already a fix up for review in #12026: on the server the GC timer isn't scheduled for any gcTime. A review there would help more than a second fix.

  2. there are valid use-cases for scheduling GC on the server; the current behaviour is also documented.

  3. TkDodo closed this

  4. TkDodo added labelswontfix

  5. TkDodo added labelshas workaround

Sign in to join this conversation.