Skip to content

Commit

Permalink
Make mHybridData thread safe for EventEmitterWrapper
Browse files Browse the repository at this point in the history
Summary:
In T94154173, when calling ```EventEmitterWrapper->invoke()```, hybrid function ```invokeEvent``` is null, even if we checked that ```mHybridData``` is valid before calling ```invokeEvent```.

**Theory:**

```invoke()``` is called from ```mqt_js``` thread, ```desotry()``` is called from ```main``` thread, which cause multi-thread access of```mHybridData```.

So if ```desotry()``` is called after ```isValid()``` check and before calling ```invokeEvent()```, ```invokeEvent``` could be destroyed and is null.

I can reproduce with above theory:

{F633411001}

**Fix:**

Make functions synchronized so ```mHybridData``` can be thread safe.

Changelog:
[Android][Fixed] - Make mHybridData thread safe

Reviewed By: RSNara

Differential Revision: D29792453

fbshipit-source-id: 8b4c754d53ece933be7b2cf99c6cd026b39e24ad
  • Loading branch information
luluwu2032 authored and facebook-github-bot committed Jul 26, 2021
1 parent 7460841 commit 7929551
Showing 1 changed file with 3 additions and 3 deletions.
Expand Up @@ -47,7 +47,7 @@ private native void invokeUniqueEvent(
* @param eventName {@link String} name of the event to execute.
* @param params {@link WritableMap} payload of the event
*/
public void invoke(@NonNull String eventName, @Nullable WritableMap params) {
public synchronized void invoke(@NonNull String eventName, @Nullable WritableMap params) {
if (!isValid()) {
return;
}
Expand All @@ -62,7 +62,7 @@ public void invoke(@NonNull String eventName, @Nullable WritableMap params) {
* @param eventName {@link String} name of the event to execute.
* @param params {@link WritableMap} payload of the event
*/
public void invokeUnique(
public synchronized void invokeUnique(
@NonNull String eventName, @Nullable WritableMap params, int customCoalesceKey) {
if (!isValid()) {
return;
Expand All @@ -71,7 +71,7 @@ public void invokeUnique(
invokeUniqueEvent(eventName, payload, customCoalesceKey);
}

public void destroy() {
public synchronized void destroy() {
if (mHybridData != null) {
mHybridData.resetNative();
}
Expand Down

0 comments on commit 7929551

Please sign in to comment.