Fix useLiveRxQuery leaking the live query subscription on unmount - #8971
Merged
pubkey merged 2 commits intoAug 19, 2026
Conversation
useRxQueryBase started query.$ inside an async runQuery callback, so useEffect never received the unsubscribe function. Return that cleanup from the effect the same way useRxDocument already does. Reported by chris-rnwbl in pubkey#8964.
On the non-live path runQuery ends with rxQuery.exec().then(), so return runQuery() handed React a Promise. Only return a cleanup function when runQuery actually produced one.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR contains:
A BUGFIX and IMPROVED TESTS.
Reported by chris-rnwbl in #8964.
Fixes #8964.
Describe the problem you have without this PR
useLiveRxQuery/useRxQueryBasesubscribed torxQuery.$inside anasyncrunQuerycallback and returned() => subscription.unsubscribe()from that callback. ReactuseEffectonly uses a function returned synchronously as cleanup. The Promise resolved value was ignored, so unmount (and query changes) never unsubscribed.useRxDocumentin the same plugin already returns cleanup fromuseEffectcorrectly.Fix
Drop
asyncfromrunQueryso the live path can return unsubscribe synchronously.useEffectnowreturn runQuery(). The non-liveexec()path uses.then()/ rejection handler instead ofawait.This does not change the
useRxDocumentfirst-paintloadingbehavior from #8965.Test case
should unsubscribe from the live query when the hook is unmountedintest/react/react-hooks.test.tsx:useLiveRxQuerycountRxQuerySubscribers(rxQuery) === 1countRxQuerySubscribers(rxQuery) === 0That assertion failed on unmodified
master(1 !== 0) and passes with this change.Todos