Skip to content

Commit 1ef83c8

Browse files
ofrobotsjasnell
authored andcommitted
trace_events: destroy platform before tracing
For safer shutdown, we should destroy the platform – and background threads - before the tracing infrastructure is destroyed. This change fixes the relative order of NodePlatform disposition and the tracing agent shutting down. This matches the nesting order for startup. Make the tracing agent own the tracing controller instead of platform to match the above. Fixes: #22865 PR-URL: #22938 Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 5055b3a commit 1ef83c8

File tree

5 files changed

+17
-14
lines changed

5 files changed

+17
-14
lines changed

src/node.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,18 @@ static struct {
292292
controller->AddTraceStateObserver(new NodeTraceStateObserver(controller));
293293
tracing::TraceEventHelper::SetTracingController(controller);
294294
StartTracingAgent();
295+
// Tracing must be initialized before platform threads are created.
295296
platform_ = new NodePlatform(thread_pool_size, controller);
296297
V8::InitializePlatform(platform_);
297298
}
298299

299300
void Dispose() {
300-
tracing_agent_.reset(nullptr);
301301
platform_->Shutdown();
302302
delete platform_;
303303
platform_ = nullptr;
304+
// Destroy tracing after the platform (and platform threads) have been
305+
// stopped.
306+
tracing_agent_.reset(nullptr);
304307
}
305308

306309
void DrainVMTasks(Isolate* isolate) {

src/node_platform.cc

+3-4
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,9 @@ int PerIsolatePlatformData::unref() {
283283
NodePlatform::NodePlatform(int thread_pool_size,
284284
TracingController* tracing_controller) {
285285
if (tracing_controller) {
286-
tracing_controller_.reset(tracing_controller);
286+
tracing_controller_ = tracing_controller;
287287
} else {
288-
TracingController* controller = new TracingController();
289-
tracing_controller_.reset(controller);
288+
tracing_controller_ = new TracingController();
290289
}
291290
worker_thread_task_runner_ =
292291
std::make_shared<WorkerThreadsTaskRunner>(thread_pool_size);
@@ -456,7 +455,7 @@ double NodePlatform::CurrentClockTimeMillis() {
456455
}
457456

458457
TracingController* NodePlatform::GetTracingController() {
459-
return tracing_controller_.get();
458+
return tracing_controller_;
460459
}
461460

462461
template <class T>

src/node_platform.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class NodePlatform : public MultiIsolatePlatform {
158158
std::unordered_map<v8::Isolate*,
159159
std::shared_ptr<PerIsolatePlatformData>> per_isolate_;
160160

161-
std::unique_ptr<v8::TracingController> tracing_controller_;
161+
v8::TracingController* tracing_controller_;
162162
std::shared_ptr<WorkerThreadsTaskRunner> worker_thread_task_runner_;
163163
};
164164

src/tracing/agent.cc

+5-6
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ using v8::platform::tracing::TraceConfig;
4848
using v8::platform::tracing::TraceWriter;
4949
using std::string;
5050

51-
Agent::Agent() {
52-
tracing_controller_ = new TracingController();
51+
Agent::Agent() : tracing_controller_(new TracingController()) {
5352
tracing_controller_->Initialize(nullptr);
5453

5554
CHECK_EQ(uv_loop_init(&tracing_loop_), 0);
@@ -117,7 +116,7 @@ AgentWriterHandle Agent::AddClient(
117116
use_categories = &categories_with_default;
118117
}
119118

120-
ScopedSuspendTracing suspend(tracing_controller_, this);
119+
ScopedSuspendTracing suspend(tracing_controller_.get(), this);
121120
int id = next_writer_id_++;
122121
AsyncTraceWriter* raw = writer.get();
123122
writers_[id] = std::move(writer);
@@ -157,7 +156,7 @@ void Agent::Disconnect(int client) {
157156
Mutex::ScopedLock lock(initialize_writer_mutex_);
158157
to_be_initialized_.erase(writers_[client].get());
159158
}
160-
ScopedSuspendTracing suspend(tracing_controller_, this);
159+
ScopedSuspendTracing suspend(tracing_controller_.get(), this);
161160
writers_.erase(client);
162161
categories_.erase(client);
163162
}
@@ -166,13 +165,13 @@ void Agent::Enable(int id, const std::set<std::string>& categories) {
166165
if (categories.empty())
167166
return;
168167

169-
ScopedSuspendTracing suspend(tracing_controller_, this,
168+
ScopedSuspendTracing suspend(tracing_controller_.get(), this,
170169
id != kDefaultHandleId);
171170
categories_[id].insert(categories.begin(), categories.end());
172171
}
173172

174173
void Agent::Disable(int id, const std::set<std::string>& categories) {
175-
ScopedSuspendTracing suspend(tracing_controller_, this,
174+
ScopedSuspendTracing suspend(tracing_controller_.get(), this,
176175
id != kDefaultHandleId);
177176
std::multiset<std::string>& writer_categories = categories_[id];
178177
for (const std::string& category : categories) {

src/tracing/agent.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ class Agent {
7070
Agent();
7171
~Agent();
7272

73-
TracingController* GetTracingController() { return tracing_controller_; }
73+
TracingController* GetTracingController() {
74+
return tracing_controller_.get();
75+
}
7476

7577
enum UseDefaultCategoryMode {
7678
kUseDefaultCategories,
@@ -121,7 +123,7 @@ class Agent {
121123
// These maps store the original arguments to AddClient(), by id.
122124
std::unordered_map<int, std::multiset<std::string>> categories_;
123125
std::unordered_map<int, std::unique_ptr<AsyncTraceWriter>> writers_;
124-
TracingController* tracing_controller_ = nullptr;
126+
std::unique_ptr<TracingController> tracing_controller_;
125127

126128
// Variables related to initializing per-event-loop properties of individual
127129
// writers, such as libuv handles.

0 commit comments

Comments
 (0)