Skip to content

Commit

Permalink
domain: avoid circular memory references
Browse files Browse the repository at this point in the history
Avoid circular references that the JS engine cannot see through
because it involves an `async id` ⇒ `domain` link.

Using weak references is not a great solution, because it increases
the domain module’s dependency on internals and the added calls into
C++ may affect performance, but it seems like the least bad one.

Fixes: nodejs#23862
  • Loading branch information
addaleax committed Feb 7, 2019
1 parent ebaae77 commit 6081b5c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const {
} = require('internal/errors').codes;
const { createHook } = require('async_hooks');

// TODO(addaleax): Use a non-internal solution for this.
const kWeak = Symbol('kWeak');
const { WeakReference } = internalBinding('util');

// Overwrite process.domain with a getter/setter that will allow for more
// effective optimizations
var _domain = [null];
Expand All @@ -53,20 +57,20 @@ const asyncHook = createHook({
init(asyncId, type, triggerAsyncId, resource) {
if (process.domain !== null && process.domain !== undefined) {
// If this operation is created while in a domain, let's mark it
pairing.set(asyncId, process.domain);
pairing.set(asyncId, process.domain[kWeak]);
resource.domain = process.domain;
}
},
before(asyncId) {
const current = pairing.get(asyncId);
if (current !== undefined) { // enter domain for this cb
current.enter();
current.get().enter();
}
},
after(asyncId) {
const current = pairing.get(asyncId);
if (current !== undefined) { // exit domain for this cb
current.exit();
current.get().exit();
}
},
destroy(asyncId) {
Expand Down Expand Up @@ -167,6 +171,7 @@ class Domain extends EventEmitter {
super();

this.members = [];
this[kWeak] = new WeakReference(this);
asyncHook.enable();

this.on('removeListener', updateExceptionCapture);
Expand Down
36 changes: 36 additions & 0 deletions test/parallel/test-domain-async-id-map-leak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Flags: --expose-gc
'use strict';
const common = require('../common');
const onGC = require('../common/ongc');
const assert = require('assert');
const async_hooks = require('async_hooks');
const domain = require('domain');
const EventEmitter = require('events');

// This test makes sure that the (async id → domain) map which is part of the
// domain module does not get in the way of garbage collection.
// See: https://github.com/nodejs/node/issues/23862

let d = domain.create();
d.run(() => {
const resource = new async_hooks.AsyncResource('TestResource');
const emitter = new EventEmitter();

d.remove(emitter);
d.add(emitter);

emitter.linkToResource = resource;
assert.strictEqual(emitter.domain, d);
assert.strictEqual(resource.domain, d);

// This would otherwise be a circular chain now:
// emitter → resource → async id ⇒ domain → emitter.
// Make sure that all of these objects are released:

onGC(resource, { ongc: common.mustCall() });
onGC(d, { ongc: common.mustCall() });
onGC(emitter, { ongc: common.mustCall() });
});

d = null;
global.gc();

0 comments on commit 6081b5c

Please sign in to comment.