useLazyLoadQuery: a sibling's commit releases a suspended component's temporary retain, causing a refetch after the sibling unmounts
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.
- C2 renders and suspends on
Q1.preparetakes a temporary retain on the entry. Q1completes. C2's boundary retries; C2 reads the data (a new temporary retain replaces the first), the sibling still suspends onQ2, so C2 does not commit.- 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. - C1 unmounts. The retain count reaches zero,
SuspenseResourcedisposes the environment retention, andQueryResourcedeletes the entry. Q2completes. C2 renders again,preparemisses the cache, and a second request forQ1goes 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:
temporaryRetainkeeps a single_releaseTemporaryRetainper 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.permanentRetaincallsreleaseTemporaryRetain()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.
Source: facebook/relay