diff --git a/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.cpp b/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.cpp index 28750e02d16..7ea5fc3c782 100644 --- a/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.cpp +++ b/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.cpp @@ -29,7 +29,8 @@ Scheduler::Scheduler( const SchedulerToolbox& schedulerToolbox, UIManagerAnimationDelegate* animationDelegate, SchedulerDelegate* delegate) - : runtimeExecutor_(schedulerToolbox.runtimeExecutor), + : delegateInvalidated_(std::make_shared>(false)), + runtimeExecutor_(schedulerToolbox.runtimeExecutor), contextContainer_(schedulerToolbox.contextContainer) { // Creating a container for future `EventDispatcher` instance. eventDispatcher_ = std::make_shared>(); @@ -171,6 +172,15 @@ Scheduler::~Scheduler() { LOG(WARNING) << "Scheduler::~Scheduler() was called (address: " << this << ")."; + // Invalidate any lambdas already queued via scheduleRenderingUpdate that + // captured a raw delegate_ pointer; without this they'd dereference a + // dangling SchedulerDelegate after Scheduler teardown. (No replacement + // token is allocated here — Scheduler is going away.) + // Gated to allow controlled rollout / rollback. + if (ReactNativeFeatureFlags::enableSchedulerDelegateInvalidation()) { + *delegateInvalidated_ = true; + } + auto weakRuntimeScheduler = contextContainer_->find>( RuntimeSchedulerKey); @@ -260,6 +270,21 @@ Scheduler::findComponentDescriptorByHandle_DO_NOT_USE_THIS_IS_BROKEN( #pragma mark - Delegate void Scheduler::setDelegate(SchedulerDelegate* delegate) { + // Gated to allow controlled rollout / rollback. + if (ReactNativeFeatureFlags::enableSchedulerDelegateInvalidation() && + delegate_ != delegate) { + // Mark the *current* token invalid: any rendering-update lambda already + // queued holds a shared_ptr to this atomic and will observe `true` on + // its next read, so it no-ops instead of calling into the previous + // delegate (which the caller is about to drop). + *delegateInvalidated_ = true; + // Then install a *fresh* token (a new atomic) so lambdas captured + // against the new delegate use their own non-invalidated flag. + // Reusing the previous atomic and flipping it back to `false` would + // re-arm the in-flight lambdas — exactly the use-after-free we're + // trying to prevent — because they share the same shared_ptr. + delegateInvalidated_ = std::make_shared>(false); + } delegate_ = delegate; } @@ -288,10 +313,21 @@ void Scheduler::uiManagerDidFinishTransaction( if (!mountSynchronously) { auto surfaceId = mountingCoordinator->getSurfaceId(); + // Capture the gating flag at queue time: the lambda's decision to + // honor the invalidation guard is fixed when we enqueue, not when it + // later runs. Avoids per-invocation feature-flag reads and keeps the + // contract for an in-flight lambda stable across flag flips. + auto guardEnabled = + ReactNativeFeatureFlags::enableSchedulerDelegateInvalidation(); runtimeScheduler_->scheduleRenderingUpdate( surfaceId, [delegate = delegate_, + invalidated = delegateInvalidated_, + guardEnabled, mountingCoordinator = std::move(mountingCoordinator)]() { + if (guardEnabled && *invalidated) { + return; + } delegate->schedulerShouldRenderTransactions(mountingCoordinator); }); } else { @@ -314,12 +350,20 @@ void Scheduler::uiManagerDidDispatchCommand( "Scheduler::uiManagerDispatchCommand", "commandName", commandName); if (delegate_ != nullptr) { auto shadowView = ShadowView(*shadowNode); + // See comment in uiManagerDidFinishTransaction above for gating shape. + auto guardEnabled = + ReactNativeFeatureFlags::enableSchedulerDelegateInvalidation(); runtimeScheduler_->scheduleRenderingUpdate( shadowNode->getSurfaceId(), [delegate = delegate_, + invalidated = delegateInvalidated_, + guardEnabled, shadowView = std::move(shadowView), commandName, args]() { + if (guardEnabled && *invalidated) { + return; + } delegate->schedulerDidDispatchCommand(shadowView, commandName, args); }); } diff --git a/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.h b/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.h index cc665dfde4b..bd6de832e04 100644 --- a/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.h +++ b/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include @@ -122,6 +123,11 @@ class Scheduler final : public UIManagerDelegate { friend class SurfaceHandler; SchedulerDelegate *delegate_; + // Invalidation token captured by-value into lambdas deferred via + // runtimeScheduler_->scheduleRenderingUpdate. Set to true on delegate + // change or Scheduler destruction so a lambda that outlives its captured + // raw delegate pointer can no-op instead of dereferencing dangling memory. + std::shared_ptr> delegateInvalidated_; SharedComponentDescriptorRegistry componentDescriptorRegistry_; RuntimeExecutor runtimeExecutor_; std::shared_ptr uiManager_;