From fd3484481a5e9852b8589b65926c8658f19b20c2 Mon Sep 17 00:00:00 2001 From: Adam Comella Date: Tue, 30 Aug 2016 04:12:42 -0700 Subject: [PATCH] Fix race condition in EventDispatcher Summary: `mRCTEventEmitter` is used by 2 different threads. It's assigned on the UI thread and it's accessed on the JavaScript thread. Currently, it can be the case that the UI thread assigns `mRCTEventEmitter` and later the JS thread accesses it but still sees null. This change fixes the issue by marking the `mRCTEventEmitter` variable as `volatile` to ensure that both threads see the same value for `mRCTEventEmitter`. **Test plan (required)** This change is currently used in my team's app. We're no longer seeing a crash in `EventDispatcher`. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/9655 Differential Revision: D3790888 Pulled By: andreicoman11 fbshipit-source-id: 68cdbc74faffb36dc2bca8ad3d4a78929badbe9c --- .../com/facebook/react/uimanager/events/EventDispatcher.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.java index 590cf56d6..8bf009a9e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.java @@ -95,7 +95,7 @@ public class EventDispatcher implements LifecycleEventListener { private Event[] mEventsToDispatch = new Event[16]; private int mEventsToDispatchSize = 0; - private @Nullable RCTEventEmitter mRCTEventEmitter; + private volatile @Nullable RCTEventEmitter mRCTEventEmitter; private final ScheduleDispatchFrameCallback mCurrentFrameCallback; private short mNextEventTypeId = 0; private volatile boolean mHasDispatchScheduled = false;