8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4fe0819 commit 6967407Copy full SHA for 6967407
src/inspector/main_thread_interface.cc
@@ -316,15 +316,21 @@ void MainThreadInterface::RemoveObject(int id) {
316
}
317
318
Deletable* MainThreadInterface::GetObject(int id) {
319
- auto iterator = managed_objects_.find(id);
+ Deletable* pointer = GetObjectIfExists(id);
320
// This would mean the object is requested after it was disposed, which is
321
// a coding error.
322
- CHECK_NE(managed_objects_.end(), iterator);
323
- Deletable* pointer = iterator->second.get();
324
CHECK_NE(nullptr, pointer);
325
return pointer;
326
327
+Deletable* MainThreadInterface::GetObjectIfExists(int id) {
+ auto iterator = managed_objects_.find(id);
328
+ if (iterator == managed_objects_.end()) {
329
+ return nullptr;
330
+ }
331
+ return iterator->second.get();
332
+}
333
+
334
std::unique_ptr<StringBuffer> Utf8ToStringView(const std::string& message) {
335
icu::UnicodeString utf16 = icu::UnicodeString::fromUTF8(
336
icu::StringPiece(message.data(), message.length()));
src/inspector/main_thread_interface.h
@@ -84,6 +84,7 @@ class MainThreadInterface {
84
85
void AddObject(int handle, std::unique_ptr<Deletable> object);
86
Deletable* GetObject(int id);
87
+ Deletable* GetObjectIfExists(int id);
88
void RemoveObject(int handle);
89
90
private:
src/inspector/tracing_agent.cc
@@ -1,4 +1,5 @@
1
#include "tracing_agent.h"
2
+#include "main_thread_interface.h"
3
#include "node_internals.h"
4
5
#include "env-inl.h"
@@ -14,10 +15,76 @@ namespace protocol {
14
15
namespace {
16
using v8::platform::tracing::TraceWriter;
17
18
+class DeletableFrontendWrapper : public Deletable {
19
+ public:
20
+ explicit DeletableFrontendWrapper(
21
+ std::weak_ptr<NodeTracing::Frontend> frontend)
22
+ : frontend_(frontend) {}
23
24
+ // This should only be called from the main thread, meaning frontend should
25
+ // not be destroyed concurrently.
26
+ NodeTracing::Frontend* get() { return frontend_.lock().get(); }
27
28
+ private:
29
+ std::weak_ptr<NodeTracing::Frontend> frontend_;
30
+};
31
32
+class CreateFrontendWrapperRequest : public Request {
33
34
+ CreateFrontendWrapperRequest(int object_id,
35
36
+ : object_id_(object_id) {
37
+ frontend_wrapper_ = std::make_unique<DeletableFrontendWrapper>(frontend);
38
39
40
+ void Call(MainThreadInterface* thread) override {
41
+ thread->AddObject(object_id_, std::move(frontend_wrapper_));
42
43
44
45
+ int object_id_;
46
+ std::unique_ptr<DeletableFrontendWrapper> frontend_wrapper_;
47
48
49
+class DestroyFrontendWrapperRequest : public Request {
50
51
+ explicit DestroyFrontendWrapperRequest(int object_id)
52
+ : object_id_(object_id) {}
53
54
55
+ thread->RemoveObject(object_id_);
56
57
58
59
60
61
62
+class SendMessageRequest : public Request {
63
64
+ explicit SendMessageRequest(int object_id, const std::string& message)
65
+ : object_id_(object_id), message_(message) {}
66
67
68
+ DeletableFrontendWrapper* frontend_wrapper =
69
+ static_cast<DeletableFrontendWrapper*>(
70
+ thread->GetObjectIfExists(object_id_));
71
+ if (frontend_wrapper == nullptr) return;
72
+ auto frontend = frontend_wrapper->get();
73
+ if (frontend != nullptr) {
74
+ frontend->sendRawNotification(message_);
75
76
77
78
79
80
+ std::string message_;
81
82
83
class InspectorTraceWriter : public node::tracing::AsyncTraceWriter {
public:
- explicit InspectorTraceWriter(NodeTracing::Frontend* frontend)
- : frontend_(frontend) {}
+ explicit InspectorTraceWriter(int frontend_object_id,
+ std::shared_ptr<MainThreadHandle> main_thread)
+ : frontend_object_id_(frontend_object_id), main_thread_(main_thread) {}
void AppendTraceEvent(
v8::platform::tracing::TraceObject* trace_event) override {
@@ -35,27 +102,35 @@ class InspectorTraceWriter : public node::tracing::AsyncTraceWriter {
102
std::ostringstream::ate);
103
result << stream_.str();
104
result << "}";
- frontend_->sendRawNotification(result.str());
105
+ main_thread_->Post(std::make_unique<SendMessageRequest>(frontend_object_id_,
106
+ result.str()));
107
stream_.str("");
108
109
110
111
std::unique_ptr<TraceWriter> json_writer_;
112
std::ostringstream stream_;
- NodeTracing::Frontend* frontend_;
113
+ int frontend_object_id_;
114
+ std::shared_ptr<MainThreadHandle> main_thread_;
115
};
116
} // namespace
117
-TracingAgent::TracingAgent(Environment* env)
- : env_(env) {
-}
118
+TracingAgent::TracingAgent(Environment* env,
119
120
+ : env_(env), main_thread_(main_thread) {}
121
122
TracingAgent::~TracingAgent() {
123
trace_writer_.reset();
124
+ main_thread_->Post(
125
+ std::make_unique<DestroyFrontendWrapperRequest>(frontend_object_id_));
126
127
128
void TracingAgent::Wire(UberDispatcher* dispatcher) {
- frontend_.reset(new NodeTracing::Frontend(dispatcher->channel()));
129
+ // Note that frontend is still owned by TracingAgent
130
+ frontend_ = std::make_shared<NodeTracing::Frontend>(dispatcher->channel());
131
+ frontend_object_id_ = main_thread_->newObjectId();
132
+ main_thread_->Post(std::make_unique<CreateFrontendWrapperRequest>(
133
+ frontend_object_id_, frontend_));
134
NodeTracing::Dispatcher::wire(dispatcher, this);
135
136
@@ -81,11 +156,11 @@ DispatchResponse TracingAgent::start(
156
157
tracing::AgentWriterHandle* writer = GetTracingAgentWriter();
158
if (writer != nullptr) {
- trace_writer_ = writer->agent()->AddClient(
- categories_set,
- std::unique_ptr<InspectorTraceWriter>(
- new InspectorTraceWriter(frontend_.get())),
- tracing::Agent::kIgnoreDefaultCategories);
159
+ trace_writer_ =
160
+ writer->agent()->AddClient(categories_set,
161
+ std::make_unique<InspectorTraceWriter>(
162
+ frontend_object_id_, main_thread_),
163
+ tracing::Agent::kIgnoreDefaultCategories);
164
165
return DispatchResponse::OK();
91
166
src/inspector/tracing_agent.h
@@ -10,11 +10,13 @@ namespace node {
10
class Environment;
11
12
namespace inspector {
13
+class MainThreadHandle;
namespace protocol {
A3E2 class TracingAgent : public NodeTracing::Backend {
- explicit TracingAgent(Environment*);
+ explicit TracingAgent(Environment*, std::shared_ptr<MainThreadHandle>);
~TracingAgent() override;
void Wire(UberDispatcher* dispatcher);
@@ -29,8 +31,10 @@ class TracingAgent : public NodeTracing::Backend {
void DisconnectTraceClient();
Environment* env_;
tracing::AgentWriterHandle trace_writer_;
- std::unique_ptr<NodeTracing::Frontend> frontend_;
+ std::shared_ptr<NodeTracing::Frontend> frontend_;
src/inspector_agent.cc
@@ -211,14 +211,15 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
211
const std::unique_ptr<V8Inspector>& inspector,
212
std::shared_ptr<WorkerManager> worker_manager,
213
std::unique_ptr<InspectorSessionDelegate> delegate,
214
+ std::shared_ptr<MainThreadHandle> main_thread_,
215
bool prevent_shutdown)
- : delegate_(std::move(delegate)),
216
- prevent_shutdown_(prevent_shutdown) {
+ : delegate_(std::move(delegate)), prevent_shutdown_(prevent_shutdown) {
217
session_ = inspector->connect(1, this, StringView());
218
- node_dispatcher_.reset(new protocol::UberDispatcher(this));
219
- tracing_agent_.reset(new protocol::TracingAgent(env));
+ node_dispatcher_ = std::make_unique<protocol::UberDispatcher>(this);
+ tracing_agent_ =
220
+ std::make_unique<protocol::TracingAgent>(env, main_thread_);
221
tracing_agent_->Wire(node_dispatcher_.get());
- worker_agent_.reset(new protocol::WorkerAgent(worker_manager));
222
+ worker_agent_ = std::make_unique<protocol::WorkerAgent>(worker_manager);
223
worker_agent_->Wire(node_dispatcher_.get());
224
225
@@ -467,9 +468,12 @@ class NodeInspectorClient : public V8InspectorClient {
467
468
bool prevent_shutdown) {
469
events_dispatched_ = true;
470
int session_id = next_session_id_++;
- channels_[session_id] =
471
- std::make_unique<ChannelImpl>(env_, client_, getWorkerManager(),
472
- std::move(delegate), prevent_shutdown);
+ channels_[session_id] = std::make_unique<ChannelImpl>(env_,
+ client_,
473
+ getWorkerManager(),
474
+ std::move(delegate),
475
+ getThreadHandle(),
476
+ prevent_shutdown);
477
return session_id;
478
479
test/parallel/test-trace-events-dynamic-enable-workers-disabled.js
@@ -25,3 +25,4 @@ session.post('NodeTracing.start', {
'Tracing properties can only be changed through main thread sessions'
});
}));
+session.disconnect();