diff --git a/lib/domain.js b/lib/domain.js index e8ae3ff1003486..02e246f2729499 100644 --- a/lib/domain.js +++ b/lib/domain.js @@ -35,7 +35,11 @@ const { } = require('internal/errors').codes; const { createHook } = require('async_hooks'); -// overwrite process.domain with a getter/setter that will allow for more +// 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]; Object.defineProperty(process, 'domain', { @@ -52,8 +56,8 @@ const pairing = new Map(); 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); + // If this operation is created while in a domain, let's mark it + pairing.set(asyncId, process.domain[kWeak]); resource.domain = process.domain; if (resource.promise !== undefined && resource.promise instanceof Promise) { @@ -67,13 +71,15 @@ const asyncHook = createHook({ before(asyncId) { const current = pairing.get(asyncId); if (current !== undefined) { // enter domain for this cb - current.enter(); + // We will get the domain through current.get(), because the resource + // object's .domain property makes sure it is not garbage collected. + 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) { @@ -174,6 +180,7 @@ class Domain extends EventEmitter { super(); this.members = []; + this[kWeak] = new WeakReference(this); asyncHook.enable(); this.on('removeListener', updateExceptionCapture); diff --git a/test/parallel/test-domain-async-id-map-leak.js b/test/parallel/test-domain-async-id-map-leak.js new file mode 100644 index 00000000000000..e720241841d130 --- /dev/null +++ b/test/parallel/test-domain-async-id-map-leak.js @@ -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();