Skip to content

Commit 78a8fa3

Browse files
committed
src: avoid V8 worker hang during platform shutdown
Signed-off-by: Archkon <180910180+Archkon@users.noreply.github.com>
1 parent 51dbfd6 commit 78a8fa3

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/node_platform.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,12 @@ 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+
}
534+
529535
worker_thread_task_runner_->Shutdown();
530536

531537
{
@@ -717,6 +723,25 @@ bool NodePlatform::FlushForegroundTasks(Isolate* isolate) {
717723
return per_isolate->FlushForegroundTasksInternal();
718724
}
719725

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