Skip to content

Commit

Permalink
src: cleanup per-isolate state on platform on isolate unregister
Browse files Browse the repository at this point in the history
Clean up once all references to an `Isolate*` are gone from the
`NodePlatform`, rather than waiting for the `PerIsolatePlatformData`
struct to be deleted since there may be cyclic references between
that struct and the individual tasks.

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
  • Loading branch information
addaleax authored and targos committed Jun 13, 2018
1 parent 9047c81 commit de7403f
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ void PerIsolatePlatformData::PostIdleTask(std::unique_ptr<v8::IdleTask> task) {
}

void PerIsolatePlatformData::PostTask(std::unique_ptr<Task> task) {
CHECK_NE(flush_tasks_, nullptr);
foreground_tasks_.Push(std::move(task));
uv_async_send(flush_tasks_);
}

void PerIsolatePlatformData::PostDelayedTask(
std::unique_ptr<Task> task, double delay_in_seconds) {
CHECK_NE(flush_tasks_, nullptr);
std::unique_ptr<DelayedTask> delayed(new DelayedTask());
delayed->task = std::move(task);
delayed->platform_data = shared_from_this();
Expand All @@ -97,13 +99,21 @@ void PerIsolatePlatformData::PostDelayedTask(
}

PerIsolatePlatformData::~PerIsolatePlatformData() {
Shutdown();
}

void PerIsolatePlatformData::Shutdown() {
if (flush_tasks_ == nullptr)
return;

while (FlushForegroundTasksInternal()) {}
CancelPendingDelayedTasks();

uv_close(reinterpret_cast<uv_handle_t*>(flush_tasks_),
[](uv_handle_t* handle) {
delete reinterpret_cast<uv_async_t*>(handle);
});
flush_tasks_ = nullptr;
}

void PerIsolatePlatformData::ref() {
Expand Down Expand Up @@ -144,6 +154,7 @@ void NodePlatform::UnregisterIsolate(IsolateData* isolate_data) {
std::shared_ptr<PerIsolatePlatformData> existing = per_isolate_[isolate];
CHECK(existing);
if (existing->unref() == 0) {
existing->Shutdown();
per_isolate_.erase(isolate);
}
}
Expand Down

0 comments on commit de7403f

Please sign in to comment.