mirror of
https://github.com/nodejs/node.git
synced 2025-05-03 16:34:41 +00:00

In cases where libraries create AsyncResources which may be emitting more events depending on usage, the only way to ensure that destroy is called properly is by calling it when the resource gets garbage collected. Fixes: https://github.com/nodejs/node/issues/16153 PR-URL: https://github.com/nodejs/node/pull/16998 Fixes: https://github.com/nodejs/node/issues/16153 Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
28 lines
634 B
JavaScript
28 lines
634 B
JavaScript
'use strict';
|
|
// Flags: --expose_gc
|
|
|
|
// This test ensures that userland-only AsyncResources cause a destroy event to
|
|
// be emitted when they get gced.
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const async_hooks = require('async_hooks');
|
|
|
|
const destroyedIds = new Set();
|
|
async_hooks.createHook({
|
|
destroy: common.mustCallAtLeast((asyncId) => {
|
|
destroyedIds.add(asyncId);
|
|
}, 1)
|
|
}).enable();
|
|
|
|
let asyncId = null;
|
|
{
|
|
const res = new async_hooks.AsyncResource('foobar');
|
|
asyncId = res.asyncId();
|
|
}
|
|
|
|
setImmediate(() => {
|
|
global.gc();
|
|
setImmediate(() => assert.ok(destroyedIds.has(asyncId)));
|
|
});
|