#5421·relay

useLazyLoadQuery: a sibling's commit releases a suspended component's temporary retain, causing a refetch after the sibling unmounts

Author: rbalicki2Created Sep 24, 2026Updated Sep 24, 2026

Reproduction: https://github.com/rbalicki2/relay-suspense-retain-repro (npm install && npm test; [email protected], [email protected], [email protected]).

Summary

When two components read the same lazy query, one component's commit releases the temporary retain that belongs to the other, still-suspended component. If the committed component then unmounts, the shared QueryResource entry is deleted and the operation released, and the suspended component refetches the query it had already fetched when its boundary finally resolves. With network-only it suspends a second time.

Sequence

C1 and C2 call useLazyLoadQuery(Q1, {}, { fetchPolicy: 'store-and-network' }) with the same variables, so they share one QueryResource cache entry. C2 is in a Suspense boundary together with a sibling that reads Q2.

  1. C2 renders and suspends on Q1. prepare takes a temporary retain on the entry.
  2. Q1 completes. C2's boundary retries; C2 reads the data (a new temporary retain replaces the first), the sibling still suspends on Q2, so C2 does not commit.
  3. C1 mounts with the same query. Its render takes a temporary retain, replacing C2's. Its commit calls permanentRetain, which releases the outstanding temporary retain. The entry is now held only by C1's permanent retain.
  4. C1 unmounts. The retain count reaches zero, SuspenseResource disposes the environment retention, and QueryResource deletes the entry.
  5. Q2 completes. C2 renders again, prepare misses the cache, and a second request for Q1 goes out.

The repository's test asserts two Q1 requests in that sequence and one in the control without C1.

Cause

packages/react-relay/relay-hooks/SuspenseResource.js:

  • temporaryRetain keeps a single _releaseTemporaryRetain per resource and calls the previous one before installing the new one, so an entry carries at most one temporary retain no matter how many suspended components rendered it.
  • permanentRetain calls releaseTemporaryRetain() unconditionally, so any component's commit drops that one temporary retain, including when it was taken by a different component that has not committed yet.

The glossary's Query Resource entry documents the single-component variant ("if the component which calls .prepare successfully loads a query, but suspends on a subsequent hook before committing, the data from that query can be garbage collected before the component ultimately renders"). This is the multi-component variant, where the release is triggered by a sibling's commit and unmount instead of the five-minute timer.

Expected

C2's render in step 5 finds the entry it prepared in step 2 and renders without a second network request, the same as when C1 never mounts.

Note on the reproduction

C1 is mounted and unmounted through state local to a wrapper that does not contain C2's boundary. A parent re-render would re-render the suspended boundary's children, which takes a fresh temporary retain for C2 and hides the problem.