Fix crash if native code tries to update the size of a modal view after JS has removed it

Summary:
See https://github.com/facebook/react-native/issues/10845

onSizeChanged is enqueueing a runnable from the ui thread that references a shadow view by id on the native modules thread. Since the shadow thread 'runs ahead' of the UI thread (e.g. it has a newer state of the world than the UI thread), this isn't safe. By the time that code block runs, it's possible that an update from JS has already removed that view from the shadow hierarchy (a change which would then propagate to the native hierarchy on the UI thread).

Reviewed By: AaaChiuuu

Differential Revision: D4706521

fbshipit-source-id: 0915f081068709b895f70b2edce12163b39e5456
This commit is contained in:
Andy Street 2017-03-15 07:49:53 -07:00 committed by Facebook Github Bot
parent 6a9c2445ed
commit 5873a228f4
2 changed files with 8 additions and 1 deletions

View File

@ -153,6 +153,12 @@ public class UIImplementation {
int newWidth,
int newHeight) {
ReactShadowNode cssNode = mShadowNodeRegistry.getNode(nodeViewTag);
if (cssNode == null) {
FLog.w(
ReactConstants.TAG,
"Tried to update size of non-existent tag: " + nodeViewTag);
return;
}
cssNode.setStyleWidth(newWidth);
cssNode.setStyleHeight(newHeight);

View File

@ -304,13 +304,14 @@ public class ReactModalHostView extends ViewGroup implements LifecycleEventListe
protected void onSizeChanged(final int w, final int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (getChildCount() > 0) {
final int viewTag = getChildAt(0).getId();
ReactContext reactContext = (ReactContext) getContext();
reactContext.runOnNativeModulesQueueThread(
new GuardedRunnable(reactContext) {
@Override
public void runGuarded() {
((ReactContext) getContext()).getNativeModule(UIManagerModule.class)
.updateNodeSize(getChildAt(0).getId(), w, h);
.updateNodeSize(viewTag, w, h);
}
});
}