fix(Worklets): time-of-check and time-of-use destructor safety#9790
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens destructor-time cleanup of JSI values by eliminating a time-of-check/time-of-use window when deciding whether it’s safe to run JSI destructors while a runtime may be concurrently tearing down.
Changes:
- Replace atomic “is dead” checks with mutex-protected, locked execution for RN runtime status.
- Update
SerializableRemoteFunctionand genericcleanupRuntimeAwarecleanup paths to perform “check + destroy” while holding the relevant lifetime lock. - Refactor
WorkletRuntimeRegistryfrom a boolean query API to arunWhileLockedAPI to support atomic check+action.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/react-native-worklets/Common/cpp/worklets/Tools/RNRuntimeStatus.h | Switches runtime-dead tracking from atomic reads to mutex-locked execution. |
| packages/react-native-worklets/Common/cpp/worklets/SharedItems/SerializableRemoteFunction.cpp | Uses the new locked runtime-status path to safely reset/free remote functions during destruction. |
| packages/react-native-worklets/Common/cpp/worklets/SharedItems/Serializable.h | Updates runtime-aware cleanup to use registry-locked “alive check + destroy” semantics. |
| packages/react-native-worklets/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.h | Replaces isRuntimeAlive with a runWhileLocked helper to prevent TOCTOU. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
bartlomiejbloniarz
approved these changes
Jun 30, 2026
tjzel
added a commit
that referenced
this pull request
Jul 1, 2026
## Summary Currently the way we check if the runtime is alive for safe destructor invocations has a chance to lead to crashes anyway, since the runtime can get cleaned up immediately after the check, since it lacks a locking mechanism. ## Test plan The test example from #9789 works. (cherry picked from commit 71774cb)
tjzel
added a commit
that referenced
this pull request
Jul 1, 2026
## Summary Currently the way we check if the runtime is alive for safe destructor invocations has a chance to lead to crashes anyway, since the runtime can get cleaned up immediately after the check, since it lacks a locking mechanism. ## Test plan The test example from #9789 works. (cherry picked from commit 71774cb)
This was referenced Jul 9, 2026
Merged
tjzel
added a commit
that referenced
this pull request
Jul 14, 2026
## Summary Currently the way we check if the runtime is alive for safe destructor invocations has a chance to lead to crashes anyway, since the runtime can get cleaned up immediately after the check, since it lacks a locking mechanism. ## Test plan The test example from #9789 works. (cherry picked from commit 71774cb)
tjzel
added a commit
that referenced
this pull request
Jul 14, 2026
## Summary Currently the way we check if the runtime is alive for safe destructor invocations has a chance to lead to crashes anyway, since the runtime can get cleaned up immediately after the check, since it lacks a locking mechanism. ## Test plan The test example from #9789 works. (cherry picked from commit 71774cb)
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
Currently the way we check if the runtime is alive for safe destructor invocations has a chance to lead to crashes anyway, since the runtime can get cleaned up immediately after the check, since it lacks a locking mechanism.
Test plan
The test example from #9789 works.