GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — diagnostics_channel: fix diagnostics channel memory leak · nodejs/node@1a47a7b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • diagnostics_channel.js
  • src
    • node_util.cc
    • node_util.h
  • test/parallel
    • test-diagnostics-channel-memory-leak.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/diagnostics_channel.js‎

Copy file name to clipboardExpand all lines: lib/diagnostics_channel.js
+3Lines 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

Collapse file

‎src/node_util.cc‎

Copy file name to clipboardExpand all lines: src/node_util.cc
+9Lines 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);
Collapse file

‎src/node_util.h‎

Copy file name to clipboardExpand all lines: src/node_util.h
+1Lines 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

Collapse file

‎test/parallel/test-diagnostics-channel-memory-leak.js‎

Copy file name to clipboard
+24Lines 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 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.