Skip to content

Conversation

@wcandillon
Copy link
Contributor

@wcandillon wcandillon commented Oct 23, 2025

The goal of this PR is to fix a race condition where the RuntimeLifeCycle host object destructor would modify the RuntimeAwareCache concurrently (from the Hades thread). cc @kmagiera @Corelli-18512

Copy link
Contributor

@kmagiera kmagiera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just left one comment re the destructor updates. Otherwise looks good

if (listenersSet != listeners.end()) {
for (auto listener : listenersSet->second) {
listener->onRuntimeDestroyed(_rt);
std::unordered_set<RuntimeLifecycleListener *> listenersCopy;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't need to use the mutex in destructor. The destructor only operates on the listeners map that it owns. If the destructor runs concurrently while some other thread is executing a method on the RuntimeMonitorObject the problem lies somewhere else. The destructor should never run when the object is in use.

There isn't really any harm in keeping the locking code here, but in this case this may obscure more serious issues in the code that manages the object.

I understand the crash from https://gist.github.com/wcandillon/041931402613e2be876a9c1e854d4c49 is only due to concurrent access between add and removeListener methods so this change in the destructor isn't needed, right?

If you want to keep the locking here you could just move all the elements from listeners to listenersCopy using a one-liner:

listenersCopy  = std::move(listeners);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The destructor modifies listeners directly, what if another runtime is added/removed concurrently to this map?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Sorry, missed the fact this was a static field. Please discard that commend

@kmagiera
Copy link
Contributor

Btw: not sure which version of RN you're targeting but the whole runtime lifecycle monitor could be replaced by a new mechanism added to JSI for RN 0.81: facebook/react-native@891ee78

@wcandillon
Copy link
Contributor Author

@kmagiera thks for pointing me to setRuntimeData and getRuntimeData. We could definitely use for v3 (graphite) but I just realized we might not need it at all if we use native states

@wcandillon wcandillon requested a review from kmagiera October 24, 2025 20:32
@wcandillon wcandillon merged commit 6f3e0f9 into Shopify:main Oct 25, 2025
13 checks passed
@github-actions
Copy link
Contributor

🎉 This PR is included in version 2.3.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants