fix(Worklets): RemoteFunction memory cycle#9673
Merged
Merged
Conversation
tjzel
marked this pull request as draft
June 16, 2026 12:59
tjzel
marked this pull request as ready for review
June 16, 2026 14:04
bartlomiejbloniarz
approved these changes
Jun 16, 2026
pull Bot
pushed a commit
to BasixKOR/react-native-reanimated
that referenced
this pull request
Jul 1, 2026
…date status (software-mansion#9789) ## Summary Fixes software-mansion#9751 Changing (again) how RN Runtime remote functions (RFs) are managed, since changes from software-mansion#9673 and software-mansion#9272 were actually insufficient and the map approach won't work. It's impossible to keep both the actual callback and cached serialized callback value alive without a memory cycle, so I'm dropping cache consistency. This might result in multiple serializations of a given RF the GC triggered and the serialized value wasn't saved, but the serialization of a RF is actually cheap. I'm dropping the map approach entirely in favor of the behavior where destructor invocation is based upon the information if the runtime is still alive. We can't directly check it, so I'm depending on the `invalidate()` method of Turbo Modules - which is supposed to be called when the runtime is still alive. ## Test plan You can test it on [df3e344](software-mansion@df3e344) commit - there's a special registry there that artificially extends the lifetime of Remote Functions - click some buttons on the provided example, then reload the app with R button. The callbacks with persist and will crash on destructor invocation without the check for `isDead`.
tjzel
added a commit
that referenced
this pull request
Jul 1, 2026
…date status (#9789) ## Summary Fixes #9751 Changing (again) how RN Runtime remote functions (RFs) are managed, since changes from #9673 and #9272 were actually insufficient and the map approach won't work. It's impossible to keep both the actual callback and cached serialized callback value alive without a memory cycle, so I'm dropping cache consistency. This might result in multiple serializations of a given RF the GC triggered and the serialized value wasn't saved, but the serialization of a RF is actually cheap. I'm dropping the map approach entirely in favor of the behavior where destructor invocation is based upon the information if the runtime is still alive. We can't directly check it, so I'm depending on the `invalidate()` method of Turbo Modules - which is supposed to be called when the runtime is still alive. ## Test plan You can test it on [df3e344](df3e344) commit - there's a special registry there that artificially extends the lifetime of Remote Functions - click some buttons on the provided example, then reload the app with R button. The callbacks with persist and will crash on destructor invocation without the check for `isDead`. (cherry picked from commit 325a644)
tjzel
added a commit
that referenced
this pull request
Jul 14, 2026
…date status (#9789) ## Summary Fixes #9751 Changing (again) how RN Runtime remote functions (RFs) are managed, since changes from #9673 and #9272 were actually insufficient and the map approach won't work. It's impossible to keep both the actual callback and cached serialized callback value alive without a memory cycle, so I'm dropping cache consistency. This might result in multiple serializations of a given RF the GC triggered and the serialized value wasn't saved, but the serialization of a RF is actually cheap. I'm dropping the map approach entirely in favor of the behavior where destructor invocation is based upon the information if the runtime is still alive. We can't directly check it, so I'm depending on the `invalidate()` method of Turbo Modules - which is supposed to be called when the runtime is still alive. ## Test plan You can test it on [df3e344](df3e344) commit - there's a special registry there that artificially extends the lifetime of Remote Functions - click some buttons on the provided example, then reload the app with R button. The callbacks with persist and will crash on destructor invocation without the check for `isDead`. (cherry picked from commit 325a644)
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.
Summary
Fixes #9661
In #9272 which was supposed to fix memory issues with Remote Function, I accidentally introduced a memory-leak due to reference retain cycle.
Before
Basically, RemoteFunctionRegistry would hold a strong reference to the (remote) function, which in turn would hold a strong reference to the serialized remote function, which would hold the C++ value.
This C++ value would erase the original function from the registry, but only in its destructor - which would never happen, because the registry itself would keep it alive forever.
flowchart TB subgraph RN[RN Runtime] R[RemoteFunctionRegistry] F[remote function] C[serialized remote function] end subgraph CPP[C++ heap] S[C++ value] end subgraph WK[Worker Runtime] H[holderFunction] end R --> F F -.-> C C --> S H --> S S ==>|"registry.delete(id) — never invoked"| R linkStyle 0,2,3 stroke:#2f9e44,stroke-width:2px linkStyle 1 stroke:#868e96,stroke-width:2px linkStyle 4 stroke:#e03131,stroke-width:3pxAfter
To fix this issue, I split the C++ Remote Function for RN callbacks into two subclasses - one would only be owned by the RN Runtime, and the other one from Worklet Runtimes.
If all Worklet Runtime references would get cleaned up, then they would schedule cleanup on the RN Runtime C++ side, to erase the original function from the registry. Then, this would in turn release the cache value and the remaining C++ value.
flowchart TB subgraph RN[RN Runtime] R[RemoteFunctionRegistry] F[remote function] C[serialized remote function] end subgraph CPP[C++ heap] O[RN-owned C++ value] P[Worklet-owned C++ value] end subgraph WK[Worker Runtime] H[holderFunction] end R --> F F -.-> C C --> O H --> P P --> O O -.-> P P ==>|"registry.delete(id)"| R linkStyle 0,2,3,4 stroke:#2f9e44,stroke-width:2px linkStyle 1,5 stroke:#868e96,stroke-width:2px linkStyle 6 stroke:#2f9e44,stroke-width:3pxTest Plan
I used the following code and was tracking constructor and destructor invocations of
SerializableRemoteFunctionin debugger.Code