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

Summary:
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.

Differential Revision: D58415684
  • Loading branch information
motiz88 authored and facebook-github-bot committed Jun 11, 2024
1 parent 53dda9e commit 317ff78
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 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];
if (_inspectorPageId.has_value()) {
facebook::react::jsinspector_modern::getInspectorInstance().removePage(*_inspectorPageId);
_inspectorPageId.reset();
_inspectorTarget.reset();

// `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 (self->_inspectorPageId.has_value()) {
// Since we can't keep using `self` after dealloc, steal its inspector
// state into block-mutable variables
__block auto inspectorPageId = std::move(self->_inspectorPageId);
__block auto inspectorTarget = std::move(self->_inspectorTarget);
RCTExecuteOnMainQueue(^{
facebook::react::jsinspector_modern::getInspectorInstance().removePage(*inspectorPageId);
inspectorPageId.reset();
inspectorTarget.reset();
});
}
}

Expand Down

0 comments on commit 317ff78

Please sign in to comment.