Skip to content

Commit

Permalink
Schedule CatalystInstanceImpl destruction using new thread
Browse files Browse the repository at this point in the history
Summary:
We currently go via the UI thread, so we can use AsyncTask to schedule the final bit of async ReactContext destruction. This is a requirement for the AsyncTask API, which is also deprecated. We should figure out a better way to schedule and re-use threads across React Native Android, but until then, we can just create a new Thread here, which is also what we do for instance creation.

Changelog: [Internal]

Differential Revision: D51706689
  • Loading branch information
javache authored and facebook-github-bot committed Nov 30, 2023
1 parent dc60d62 commit e84f408
Showing 1 changed file with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;

import android.content.res.AssetManager;
import android.os.AsyncTask;
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
Expand Down Expand Up @@ -374,30 +373,24 @@ public void destroy() {
mTurboModuleRegistry.invalidate();
}

getReactQueueConfiguration()
.getUIQueueThread()
.runOnQueue(
// Kill non-UI threads from neutral third party
// potentially expensive, so don't run on UI thread
new Thread(
() -> {
// AsyncTask.execute must be executed from the UI Thread
AsyncTask.execute(
() -> {
// Kill non-UI threads from neutral third party
// potentially expensive, so don't run on UI thread

// contextHolder is used as a lock to guard against
// other users of the JS VM having the VM destroyed
// underneath them, so notify them before we reset
// Native
mJavaScriptContextHolder.clear();

mHybridData.resetNative();
getReactQueueConfiguration().destroy();
FLog.d(
ReactConstants.TAG, "CatalystInstanceImpl.destroy() end");
ReactMarker.logMarker(
ReactMarkerConstants.DESTROY_CATALYST_INSTANCE_END);
});
});
// contextHolder is used as a lock to guard against
// other users of the JS VM having the VM destroyed
// underneath them, so notify them before we reset
// Native
mJavaScriptContextHolder.clear();

mHybridData.resetNative();
getReactQueueConfiguration().destroy();
FLog.w(ReactConstants.TAG, "CatalystInstanceImpl.destroy() end");
ReactMarker.logMarker(
ReactMarkerConstants.DESTROY_CATALYST_INSTANCE_END);
},
"destroy_react_context")
.start();
});
});

Expand Down

0 comments on commit e84f408

Please sign in to comment.