Skip to content

Commit 9ba3bf1

Browse files
committed
Make sure GetContext bails out when SetContext is mutating the CPED
1 parent e5129df commit 9ba3bf1

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

bindings/profilers/wall.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,8 +1051,12 @@ void WallProfiler::SetContext(Isolate* isolate, Local<Value> value) {
10511051
if (profData->IsUndefined()) {
10521052
contextPtr = new PersistentContextPtr();
10531053

1054-
auto maybeSetResult =
1055-
cpedObj->Set(v8Ctx, localSymbol, External::New(isolate, contextPtr));
1054+
auto external = External::New(isolate, contextPtr);
1055+
setInProgress.store(true, std::memory_order_relaxed);
1056+
std::atomic_signal_fence(std::memory_order_release);
1057+
auto maybeSetResult = cpedObj->Set(v8Ctx, localSymbol, external);
1058+
std::atomic_signal_fence(std::memory_order_release);
1059+
setInProgress.store(false, std::memory_order_relaxed);
10561060
if (maybeSetResult.IsNothing()) {
10571061
delete contextPtr;
10581062
return;
@@ -1076,6 +1080,14 @@ ContextPtr WallProfiler::GetContextPtrSignalSafe(Isolate* isolate) {
10761080
return curContext_.Get();
10771081
}
10781082

1083+
auto isSetInProgress = setInProgress.load(std::memory_order_relaxed);
1084+
std::atomic_signal_fence(std::memory_order_acquire);
1085+
if (isSetInProgress) {
1086+
// SetContext is just calling Object::Set on the CPED object, safe behavior
1087+
// is to not try attempt Object::Get on it and just return empty right now.
1088+
return std::shared_ptr<Global<Value>>();
1089+
}
1090+
10791091
auto curGcCount = gcCount.load(std::memory_order_relaxed);
10801092
std::atomic_signal_fence(std::memory_order_acquire);
10811093
if (curGcCount > 0) {

bindings/profilers/wall.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class WallProfiler : public Nan::ObjectWrap {
8989
v8::Global<v8::Symbol> cpedSymbol_;
9090

9191
std::atomic<int> gcCount = 0;
92+
std::atomic<bool> setInProgress = false;
9293
double gcAsyncId;
9394
ContextPtr gcContext;
9495

0 commit comments

Comments
 (0)