Skip to content

Commit 3be237b

Browse files
committed
Add mutex for map access
1 parent 70cd10a commit 3be237b

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

module.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ using namespace node;
99

1010
static const int kMaxStackFrames = 255;
1111

12+
static std::mutex threads_mutex;
1213
static std::unordered_map<v8::Isolate *, int> threads = {};
1314

1415
static void ExecutionInterrupted(Isolate *isolate, void *data)
@@ -78,23 +79,17 @@ std::string CaptureStackTrace(Isolate *isolate)
7879

7980
void 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)
122117
void 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

Comments
 (0)