-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Memory mismanagement with UI scheduled callbacks #6900
Merged
Conversation
This file contains 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
tjzel
commented
Jan 20, 2025
packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.cpp
Show resolved
Hide resolved
packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.cpp
Show resolved
Hide resolved
packages/react-native-reanimated/apple/reanimated/apple/native/NativeProxy.mm
Show resolved
Hide resolved
tjzel
commented
Jan 20, 2025
packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.cpp
Outdated
Show resolved
Hide resolved
bartlomiejbloniarz
approved these changes
Jan 21, 2025
tjzel
commented
Jan 22, 2025
packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.h
Show resolved
Hide resolved
packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp
Outdated
Show resolved
Hide resolved
tomekzaw
reviewed
Jan 22, 2025
packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.cpp
Outdated
Show resolved
Hide resolved
MatiPl01
approved these changes
Jan 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Now it runs on iOS as well, so, for me, it's good to go.
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
What
The pattern we used with scheduling callbacks on the UI thread - capturing
this
as a reference - is prone to memory issues. Given such code:We are likely to run into accessing invalidated memory during a reload. This is due to fact that
WorkletsModuleProxy
is managed by some object held by the instance of React Native. Let's look at the following scenario.WorkletsModuleProxy
is created on the JS thread and held by theWorkletsModule
Native Module.WorkletsModuleProxy::scheduleOnUI
is invoked on the JS thread. The callback is scheduled to be executed on the UI thread.WorkletsModule
gets destroyed. Therefore,WorkletsModuleProxy
is released and also destroyed.this
has been invalidated. The App crashes.Keep in mind that this isn't exclusive to thread jumps exclusively. Calling
scheduleOnUI
on the UI thread could still result in the callback executing after the memory has been invalidated.WorkletsModuleProxy
is only an example here, the problem could manifest in all the places where we pass lambdas that capturethis
by reference.Fix
To fix this I refactored the code so everytime we pass
this
to a scheduled callback, it would be done via a weak pointer which would lock the object and prevent it from being destroyed while the callback is being executed on the UI thread.Perhaps some bits of code don't need this safety measure due to a heuristic existing that guarantees that respective memory won't be invalidated before the callback gets executed. However, I found it extremely challenging and unreliable to come up with these heuristics, as they could possibly break at any future change of the code.
Affected code:
Test plan
Reloading the app no longer causes a crash on a scheduled UI callback.