-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
domain: avoid circular memory references
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. PR-URL: #25993 Fixes: #23862 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Π‘ΠΊΠΎΠ²ΠΎΡΠΎΠ΄Π° ΠΠΈΠΊΠΈΡΠ° ΠΠ½Π΄ΡΠ΅Π΅Π²ΠΈΡ <[email protected]> Reviewed-By: Vladimir de Turckheim <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
- Loading branch information
Showing
2 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |