Skip to content

Commit f47adfb

Browse files
joyeecheungdanbev
authored andcommitted
src: fix DTrace GC callbacks DCHECKs and add cleanup
Use the variant of GC callbacks that takes data to avoid running into DCHECKs when multiple Environments try to add the same callback to the same isolate multiple times. In addition, remove the callbacks in the Environment cleanup hook. PR-URL: #26742 Fixes: #26736 Reviewed-By: Matheus Marchini <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 67c9f42 commit f47adfb

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/node_dtrace.cc

+18-6
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,19 @@ void DTRACE_HTTP_CLIENT_RESPONSE(const FunctionCallbackInfo<Value>& args) {
248248
NODE_HTTP_CLIENT_RESPONSE(&conn, conn.remote, conn.port, conn.fd);
249249
}
250250

251-
252-
void dtrace_gc_start(Isolate* isolate, GCType type, GCCallbackFlags flags) {
251+
void dtrace_gc_start(Isolate* isolate,
252+
GCType type,
253+
GCCallbackFlags flags,
254+
void* data) {
253255
// Previous versions of this probe point only logged type and flags.
254256
// That's why for reasons of backwards compatibility the isolate goes last.
255257
NODE_GC_START(type, flags, isolate);
256258
}
257259

258-
259-
void dtrace_gc_done(Isolate* isolate, GCType type, GCCallbackFlags flags) {
260+
void dtrace_gc_done(Isolate* isolate,
261+
GCType type,
262+
GCCallbackFlags flags,
263+
void* data) {
260264
// Previous versions of this probe point only logged type and flags.
261265
// That's why for reasons of backwards compatibility the isolate goes last.
262266
NODE_GC_DONE(type, flags, isolate);
@@ -272,8 +276,16 @@ void InitDTrace(Environment* env) {
272276
}
273277
#endif
274278

275-
env->isolate()->AddGCPrologueCallback(dtrace_gc_start);
276-
env->isolate()->AddGCEpilogueCallback(dtrace_gc_done);
279+
// We need to use the variant of GC callbacks that takes data to
280+
// avoid running into DCHECKs when multiple Environments try to add
281+
// the same callback to the same isolate multiple times.
282+
env->isolate()->AddGCPrologueCallback(dtrace_gc_start, env);
283+
env->isolate()->AddGCEpilogueCallback(dtrace_gc_done, env);
284+
env->AddCleanupHook([](void* data) {
285+
Environment* env = static_cast<Environment*>(data);
286+
env->isolate()->RemoveGCPrologueCallback(dtrace_gc_start, env);
287+
env->isolate()->RemoveGCEpilogueCallback(dtrace_gc_done, env);
288+
}, env);
277289
}
278290

279291
void InitializeDTrace(Local<Object> target,

0 commit comments

Comments
 (0)