Skip to content

Commit acea541

Browse files
committed
src: avoid V8 worker hang during platform shutdown
1 parent 51dbfd6 commit acea541

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/node_platform.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,11 @@ void NodePlatform::AddIsolateFinishedCallback(Isolate* isolate,
526526
void NodePlatform::Shutdown() {
527527
if (has_shut_down_) return;
528528
has_shut_down_ = true;
529+
530+
// V8 background tasks may be parked waiting for foreground GC work before
531+
// they can return from the worker thread task.
532+
while (FlushForegroundTasksForAllIsolates()) {}
533+
529534
worker_thread_task_runner_->Shutdown();
530535

531536
{
@@ -717,6 +722,25 @@ bool NodePlatform::FlushForegroundTasks(Isolate* isolate) {
717722
return per_isolate->FlushForegroundTasksInternal();
718723
}
719724

725+
bool NodePlatform::FlushForegroundTasksForAllIsolates() {
726+
std::vector<std::shared_ptr<PerIsolatePlatformData>> per_isolates;
727+
{
728+
Mutex::ScopedLock lock(per_isolate_mutex_);
729+
per_isolates.reserve(per_isolate_.size());
730+
for (const auto& entry : per_isolate_) {
731+
if (entry.second.second) {
732+
per_isolates.push_back(entry.second.second);
733+
}
734+
}
735+
}
736+
737+
bool did_work = false;
738+
for (const auto& per_isolate : per_isolates) {
739+
did_work |= per_isolate->FlushForegroundTasksInternal();
740+
}
741+
return did_work;
742+
}
743+
720744
std::unique_ptr<v8::JobHandle> NodePlatform::CreateJobImpl(
721745
v8::TaskPriority priority,
722746
std::unique_ptr<v8::JobTask> job_task,

src/node_platform.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ class NodePlatform : public MultiIsolatePlatform {
266266
private:
267267
IsolatePlatformDelegate* ForIsolate(v8::Isolate* isolate);
268268
std::shared_ptr<PerIsolatePlatformData> ForNodeIsolate(v8::Isolate* isolate);
269+
bool FlushForegroundTasksForAllIsolates();
269270

270271
Mutex per_isolate_mutex_;
271272
using DelegatePair = std::pair<IsolatePlatformDelegate*,

0 commit comments

Comments
 (0)