From 421df262661ad5ade7c190618d4a5b5bb71fef30 Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Wed, 7 Jul 2021 14:35:19 -0700 Subject: [PATCH] Mitigate T94864568 by dropping TouchEvents that do not have a MotionEvent Summary: I suspect that T94864568 is caused by TouchEvents being dispatched after they've been recycled. This needs further analysis, but to stop the bleeding, we can drop events at the point they'd be dispatched before the crash, and log a soft error. Changelog: [Internal] Reviewed By: ShikaSD Differential Revision: D29594749 fbshipit-source-id: f50df8df2125b83126616ceaf4e529127d154c7c --- .../facebook/react/uimanager/events/TouchEvent.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/TouchEvent.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/TouchEvent.java index 6d8f644411..e0509633bb 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/TouchEvent.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/TouchEvent.java @@ -11,6 +11,7 @@ import android.view.MotionEvent; import androidx.annotation.Nullable; import androidx.core.util.Pools; import com.facebook.infer.annotation.Assertions; +import com.facebook.react.bridge.ReactSoftException; import com.facebook.react.bridge.SoftAssertions; /** @@ -22,6 +23,7 @@ import com.facebook.react.bridge.SoftAssertions; * these coalescing keys are determined. */ public class TouchEvent extends Event { + private static final String TAG = TouchEvent.class.getSimpleName(); private static final int TOUCH_EVENTS_POOL_SIZE = 3; @@ -163,6 +165,13 @@ public class TouchEvent extends Event { @Override public void dispatch(RCTEventEmitter rctEventEmitter) { + if (!hasMotionEvent()) { + ReactSoftException.logSoftException( + TAG, + new IllegalStateException( + "Cannot dispatch a TouchEvent that has no MotionEvent; the TouchEvent has been recycled")); + return; + } TouchesHelper.sendTouchEvent( rctEventEmitter, Assertions.assertNotNull(mTouchEventType), @@ -181,6 +190,10 @@ public class TouchEvent extends Event { return mMotionEvent; } + private boolean hasMotionEvent() { + return mMotionEvent != null; + } + public float getViewX() { return mViewX; }