Skip to content

Commit

Permalink
Prevent Fusebox infra crash if RCTBridge is dealloc'ed off the main q…
Browse files Browse the repository at this point in the history
…ueue (#44877)

Summary:
Pull Request resolved: #44877

Changelog: [Internal]

We're seeing a sporadic iOS crash that suggests `[RCTBridge dealloc]` is being called off the main queue (despite a comment suggesting it shouldn't be). This exposes a race condition between destroying the `HostTarget` and attempting to unregister the instance+runtime from it . Here we use `RCTExecuteOnMainQueue` to make sure the `HostTarget` destruction is always sequenced after the `unregisterFromInspector()` call.

Reviewed By: huntie

Differential Revision: D58415684
  • Loading branch information
motiz88 authored and facebook-github-bot committed Jun 12, 2024
1 parent 18be49e commit f049bec
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/react-native/React/Base/RCTBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,20 @@ - (void)dealloc
// NOTE: RCTCxxBridge will use _inspectorTarget during [self invalidate], so we must
// keep it alive until after the call returns.
[self invalidate];

// `invalidate` is asynchronous if we aren't on the main queue. Unregister
// the HostTarget on the main queue so that `invalidate` can complete safely
// in that case.
if (_inspectorPageId.has_value()) {
facebook::react::jsinspector_modern::getInspectorInstance().removePage(*_inspectorPageId);
_inspectorPageId.reset();
_inspectorTarget.reset();
// Since we can't keep using `self` after dealloc, steal its inspector
// state into block-mutable variables
__block auto inspectorPageId = std::move(_inspectorPageId);
__block auto inspectorTarget = std::move(_inspectorTarget);
RCTExecuteOnMainQueue(^{
facebook::react::jsinspector_modern::getInspectorInstance().removePage(*inspectorPageId);
inspectorPageId.reset();
inspectorTarget.reset();
});
}
}

Expand Down

0 comments on commit f049bec

Please sign in to comment.