@@ -9,6 +9,7 @@ using namespace node;
99
1010static const int kMaxStackFrames = 255 ;
1111
12+ static std::mutex threads_mutex;
1213static std::unordered_map<v8::Isolate *, int > threads = {};
1314
1415static void ExecutionInterrupted (Isolate *isolate, void *data)
@@ -78,23 +79,17 @@ std::string CaptureStackTrace(Isolate *isolate)
7879
7980void CaptureStackTraces (const FunctionCallbackInfo<Value> &args)
8081{
81- bool exclude_workers = args.Length () == 1 && args[0 ]->IsBoolean () && args[0 ].As <Boolean>()->Value ();
8282 auto capture_from_isolate = args.GetIsolate ();
8383
8484 std::vector<std::future<std::string>> futures;
8585
86+ std::lock_guard<std::mutex> lock (threads_mutex);
8687 for (auto &thread : threads)
8788 {
8889 auto thread_isolate = thread.first ;
8990 if (thread_isolate != capture_from_isolate)
9091 {
9192 int thread_id = thread.second ;
92-
93- if (exclude_workers && thread_id != -1 )
94- {
95- continue ;
96- }
97-
9893 auto thread_name = thread_id == -1 ? " main" : " worker-" + std::to_string (thread_id);
9994
10095 futures.emplace_back (std::async (std::launch::async, [thread_name](Isolate *isolate)
@@ -122,6 +117,7 @@ void CaptureStackTraces(const FunctionCallbackInfo<Value> &args)
122117void Cleanup (void *arg)
123118{
124119 auto isolate = static_cast <Isolate *>(arg);
120+ std::lock_guard<std::mutex> lock (threads_mutex);
125121 threads.erase (isolate);
126122}
127123
@@ -137,7 +133,10 @@ void RegisterThread(const FunctionCallbackInfo<Value> &args)
137133
138134 int thread_id = args[0 ].As <Number>()->Value ();
139135
140- threads.emplace (isolate, thread_id);
136+ {
137+ std::lock_guard<std::mutex> lock (threads_mutex);
138+ threads.emplace (isolate, thread_id);
139+ }
141140 node::AddEnvironmentCleanupHook (isolate, Cleanup, isolate);
142141}
143142
0 commit comments