fix: avoid using v8 on Isolate termination (#35766)

* fix: avoid using v8 on Isolate termination

* chore: refactor for review

---------

Co-authored-by: electron-patch-conflict-fixer[bot] <83340002+electron-patch-conflict-fixer[bot]@users.noreply.github.com>
This commit is contained in:
Shelley Vohr 2023-02-09 08:48:49 +00:00 committed by GitHub
parent 8dc98aeeae
commit 478ce96914
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 121 additions and 108 deletions

View file

@ -74,22 +74,45 @@ struct base::trace_event::TraceValue::Helper<
namespace electron {
namespace {
gin::IsolateHolder CreateIsolateHolder(v8::Isolate* isolate) {
std::unique_ptr<v8::Isolate::CreateParams> create_params =
gin::IsolateHolder::getDefaultIsolateParams();
// Align behavior with V8 Isolate default for Node.js.
// This is necessary for important aspects of Node.js
// including heap and cpu profilers to function properly.
//
// Additional note:
// We do not want to invoke a termination exception at exit when
// we're running with only_terminate_in_safe_scope set to false. Heap and
// coverage profilers run after environment exit and if there is a pending
// exception at this stage then they will fail to generate the appropriate
// profiles. Node.js does not call node::Stop(), which calls
// isolate->TerminateExecution(), and therefore does not have this issue
// when also running with only_terminate_in_safe_scope set to false.
create_params->only_terminate_in_safe_scope = false;
return gin::IsolateHolder(
base::SingleThreadTaskRunner::GetCurrentDefault(),
gin::IsolateHolder::kSingleThread,
gin::IsolateHolder::IsolateType::kUtility, std::move(create_params),
gin::IsolateHolder::IsolateCreationMode::kNormal, isolate);
}
} // namespace
JavascriptEnvironment::JavascriptEnvironment(uv_loop_t* event_loop,
bool setup_wasm_streaming)
: isolate_(Initialize(event_loop, setup_wasm_streaming)),
isolate_holder_(base::SingleThreadTaskRunner::GetCurrentDefault(),
gin::IsolateHolder::kSingleThread,
gin::IsolateHolder::kAllowAtomicsWait,
gin::IsolateHolder::IsolateType::kUtility,
gin::IsolateHolder::IsolateCreationMode::kNormal,
nullptr,
nullptr,
isolate_),
isolate_holder_(CreateIsolateHolder(isolate_)),
locker_(isolate_) {
isolate_->Enter();
v8::HandleScope scope(isolate_);
auto context = node::NewContext(isolate_);
context_ = v8::Global<v8::Context>(isolate_, context);
CHECK(!context.IsEmpty());
context->Enter();
}
@ -99,7 +122,7 @@ JavascriptEnvironment::~JavascriptEnvironment() {
{
v8::HandleScope scope(isolate_);
context_.Get(isolate_)->Exit();
isolate_->GetCurrentContext()->Exit();
}
isolate_->Exit();
g_isolate = nullptr;