Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not hold raw RuntimeScheduler pointer in BufferedRuntimeExecutor #46542

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ ReactInstance::ReactInstance(
PerformanceEntryReporter::getInstance().get());

bufferedRuntimeExecutor_ = std::make_shared<BufferedRuntimeExecutor>(
[runtimeScheduler = runtimeScheduler_.get()](
[runtimeScheduler = runtimeScheduler_](
std::function<void(jsi::Runtime & runtime)>&& callback) {
runtimeScheduler->scheduleWork(std::move(callback));
});
Expand All @@ -156,7 +156,7 @@ void ReactInstance::unregisterFromInspector() {
}

RuntimeExecutor ReactInstance::getUnbufferedRuntimeExecutor() noexcept {
return [runtimeScheduler = runtimeScheduler_.get()](
return [runtimeScheduler = runtimeScheduler_](
std::function<void(jsi::Runtime & runtime)>&& callback) {
runtimeScheduler->scheduleWork(std::move(callback));
};
Expand All @@ -167,12 +167,14 @@ RuntimeExecutor ReactInstance::getUnbufferedRuntimeExecutor() noexcept {
// getUnbufferedRuntimeExecutor() instead if you do not need the main JS bundle
// to have finished. e.g. setting global variables into JS runtime.
RuntimeExecutor ReactInstance::getBufferedRuntimeExecutor() noexcept {
return [weakBufferedRuntimeExecutor_ =
// FIXME: we don't really need a weak reference here, bufferedRuntimeExecutor
// retains a strong reference to runtimeScheduler, which in turns retains weak
// references to the runtime
return [weakBufferedRuntimeExecutor =
std::weak_ptr<BufferedRuntimeExecutor>(bufferedRuntimeExecutor_)](
std::function<void(jsi::Runtime & runtime)>&& callback) {
if (auto strongBufferedRuntimeExecutor_ =
weakBufferedRuntimeExecutor_.lock()) {
strongBufferedRuntimeExecutor_->execute(std::move(callback));
if (auto bufferedRuntimeExecutor = weakBufferedRuntimeExecutor.lock()) {
bufferedRuntimeExecutor->execute(std::move(callback));
}
};
}
Expand Down Expand Up @@ -209,12 +211,8 @@ void ReactInstance::loadScript(
std::string scriptName = simpleBasename(sourceURL);

runtimeScheduler_->scheduleWork(
[this,
scriptName,
sourceURL,
buffer = std::move(buffer),
weakBufferedRuntimeExecuter = std::weak_ptr<BufferedRuntimeExecutor>(
bufferedRuntimeExecutor_)](jsi::Runtime& runtime) {
[this, scriptName, sourceURL, buffer = std::move(buffer)](
jsi::Runtime& runtime) {
SystraceSection s("ReactInstance::loadScript");
bool hasLogger(ReactMarker::logTaggedMarkerBridgelessImpl);
if (hasLogger) {
Expand All @@ -240,10 +238,8 @@ void ReactInstance::loadScript(
ReactMarker::INIT_REACT_RUNTIME_STOP);
ReactMarker::logMarkerBridgeless(ReactMarker::APP_STARTUP_STOP);
}
if (auto strongBufferedRuntimeExecuter =
weakBufferedRuntimeExecuter.lock()) {
strongBufferedRuntimeExecuter->flush();
}

bufferedRuntimeExecutor_->flush();
});
}

Expand Down
Loading