Skip to content

Commit

Permalink
diagnostics_channel: fix diagnostics channel memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed Nov 28, 2022
1 parent 2a7635f commit cf83822
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/diagnostics_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ function unsubscribe(name, subscription) {
}

channels[name].decRef();
if (channels[name].getRef() === 0) {
delete channels[name];
}
return true;
}

Expand Down
9 changes: 9 additions & 0 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ void WeakReference::Get(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(weak_ref->target_.Get(isolate));
}

void WeakReference::GetRef(const FunctionCallbackInfo<Value>& args) {
WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(
v8::Number::New(isolate, weak_ref->reference_count_));
}

void WeakReference::IncRef(const FunctionCallbackInfo<Value>& args) {
WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
weak_ref->reference_count_++;
Expand Down Expand Up @@ -350,6 +357,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(ArrayBufferViewHasBuffer);
registry->Register(WeakReference::New);
registry->Register(WeakReference::Get);
registry->Register(WeakReference::GetRef);
registry->Register(WeakReference::IncRef);
registry->Register(WeakReference::DecRef);
registry->Register(GuessHandleType);
Expand Down Expand Up @@ -438,6 +446,7 @@ void Initialize(Local<Object> target,
WeakReference::kInternalFieldCount);
weak_ref->Inherit(BaseObject::GetConstructorTemplate(env));
SetProtoMethod(isolate, weak_ref, "get", WeakReference::Get);
SetProtoMethod(isolate, weak_ref, "getRef", WeakReference::GetRef);
SetProtoMethod(isolate, weak_ref, "incRef", WeakReference::IncRef);
SetProtoMethod(isolate, weak_ref, "decRef", WeakReference::DecRef);
SetConstructorFunction(context, target, "WeakReference", weak_ref);
Expand Down
1 change: 1 addition & 0 deletions src/node_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class WeakReference : public SnapshotableObject {
v8::Local<v8::Object> target);
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Get(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetRef(const v8::FunctionCallbackInfo<v8::Value>& args);
static void IncRef(const v8::FunctionCallbackInfo<v8::Value>& args);
static void DecRef(const v8::FunctionCallbackInfo<v8::Value>& args);

Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-diagnostics-channel-memory-leak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
require('../common');
const assert = require('assert');

const cp = require('child_process');

const code = `
const { subscribe, unsubscribe } = require('diagnostics_channel');
function noop() {}
const before = process.memoryUsage().heapUsed;
for (let i = 0; i < 100; i++) {
subscribe(String(i), noop);
unsubscribe(String(i), noop);
}
gc();
const after = process.memoryUsage().heapUsed;
assert.ok(before >= after);
`;

const proc = cp.spawnSync(process.execPath, ['--expose-gc', '-e', code ]);

assert.ok(proc.status === 0);

0 comments on commit cf83822

Please sign in to comment.