@@ -116,7 +116,13 @@ void MarkGarbageCollectionStart(
116116 GCCallbackFlags flags,
117117 void * data) {
118118 Environment* env = static_cast <Environment*>(data);
119+ // Prevent gc callback from reentering with different type
120+ // See https://github.com/nodejs/node/issues/44046
121+ if (env->performance_state ()->current_gc_type != 0 ) {
122+ return ;
123+ }
119124 env->performance_state ()->performance_last_gc_start_mark = PERFORMANCE_NOW ();
125+ env->performance_state ()->current_gc_type = type;
120126}
121127
122128MaybeLocal<Object> GCPerformanceEntryTraits::GetDetails (
@@ -153,6 +159,10 @@ void MarkGarbageCollectionEnd(
153159 void * data) {
154160 Environment* env = static_cast <Environment*>(data);
155161 PerformanceState* state = env->performance_state ();
162+ if (type != state->current_gc_type ) {
163+ return ;
164+ }
165+ env->performance_state ()->current_gc_type = 0 ;
156166 // If no one is listening to gc performance entries, do not create them.
157167 if (LIKELY (!state->observers [NODE_PERFORMANCE_ENTRY_TYPE_GC]))
158168 return ;
@@ -178,14 +188,17 @@ void MarkGarbageCollectionEnd(
178188
179189void GarbageCollectionCleanupHook (void * data) {
180190 Environment* env = static_cast <Environment*>(data);
191+ // Reset current_gc_type to 0
192+ env->performance_state ()->current_gc_type = 0 ;
181193 env->isolate ()->RemoveGCPrologueCallback (MarkGarbageCollectionStart, data);
182194 env->isolate ()->RemoveGCEpilogueCallback (MarkGarbageCollectionEnd, data);
183195}
184196
185197static void InstallGarbageCollectionTracking (
186198 const FunctionCallbackInfo<Value>& args) {
187199 Environment* env = Environment::GetCurrent (args);
188-
200+ // Reset current_gc_type to 0
201+ env->performance_state ()->current_gc_type = 0 ;
189202 env->isolate ()->AddGCPrologueCallback (MarkGarbageCollectionStart,
190203 static_cast <void *>(env));
191204 env->isolate ()->AddGCEpilogueCallback (MarkGarbageCollectionEnd,
0 commit comments