Skip to content

Commit c527741

Browse files
theanarkhtargos
authored andcommitted
diagnostics_channel: fix diagnostics channel memory leak
PR-URL: #45633 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent c0df265 commit c527741

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

lib/diagnostics_channel.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ function unsubscribe(name, subscription) {
122122
}
123123

124124
channels[name].decRef();
125+
if (channels[name].getRef() === 0) {
126+
delete channels[name];
127+
}
125128
return true;
126129
}
127130

src/node_util.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,13 @@ void WeakReference::Get(const FunctionCallbackInfo<Value>& args) {
294294
args.GetReturnValue().Set(weak_ref->target_.Get(isolate));
295295
}
296296

297+
void WeakReference::GetRef(const FunctionCallbackInfo<Value>& args) {
298+
WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
299+
Isolate* isolate = args.GetIsolate();
300+
args.GetReturnValue().Set(
301+
v8::Number::New(isolate, weak_ref->reference_count_));
302+
}
303+
297304
void WeakReference::IncRef(const FunctionCallbackInfo<Value>& args) {
298305
WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
299306
weak_ref->reference_count_++;
@@ -391,6 +398,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
391398
registry->Register(ArrayBufferViewHasBuffer);
392399
registry->Register(WeakReference::New);
393400
registry->Register(WeakReference::Get);
401+
registry->Register(WeakReference::GetRef);
394402
registry->Register(WeakReference::IncRef);
395403
registry->Register(WeakReference::DecRef);
396404
registry->Register(GuessHandleType);
@@ -464,6 +472,7 @@ void Initialize(Local<Object> target,
464472
WeakReference::kInternalFieldCount);
465473
weak_ref->Inherit(BaseObject::GetConstructorTemplate(env));
466474
SetProtoMethod(isolate, weak_ref, "get", WeakReference::Get);
475+
SetProtoMethod(isolate, weak_ref, "getRef", WeakReference::GetRef);
467476
SetProtoMethod(isolate, weak_ref, "incRef", WeakReference::IncRef);
468477
SetProtoMethod(isolate, weak_ref, "decRef", WeakReference::DecRef);
469478
SetConstructorFunction(context, target, "WeakReference", weak_ref);

src/node_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class WeakReference : public SnapshotableObject {
2323
v8::Local<v8::Object> target);
2424
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
2525
static void Get(const v8::FunctionCallbackInfo<v8::Value>& args);
26+
static void GetRef(const v8::FunctionCallbackInfo<v8::Value>& args);
2627
static void IncRef(const v8::FunctionCallbackInfo<v8::Value>& args);
2728
static void DecRef(const v8::FunctionCallbackInfo<v8::Value>& args);
2829

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Flags: --expose-gc
2+
'use strict';
3+
4+
// This test ensures that diagnostic channel references aren't leaked.
5+
6+
require('../common');
7+
const { ok } = require('assert');
8+
9+
const { subscribe, unsubscribe } = require('diagnostics_channel');
10+
11+
function noop() {}
12+
13+
const heapUsedBefore = process.memoryUsage().heapUsed;
14+
15+
for (let i = 0; i < 1000; i++) {
16+
subscribe(String(i), noop);
17+
unsubscribe(String(i), noop);
18+
}
19+
20+
global.gc();
21+
22+
const heapUsedAfter = process.memoryUsage().heapUsed;
23+
24+
ok(heapUsedBefore >= heapUsedAfter);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy