-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
async_hooks: add destroy event for gced AsyncResources
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: #16153 PR-URL: #16998 Fixes: #16153 Reviewed-By: Andreas Madsen <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
1 parent
a1a9957
commit cf0d7cf
Showing
10 changed files
with
196 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
const { AsyncResource } = require('async_hooks'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e6], | ||
method: [ | ||
'trackingEnabled', | ||
'trackingDisabled', | ||
] | ||
}, { | ||
flags: ['--expose-gc'] | ||
}); | ||
|
||
function endAfterGC(n) { | ||
setImmediate(() => { | ||
global.gc(); | ||
setImmediate(() => { | ||
bench.end(n); | ||
}); | ||
}); | ||
} | ||
|
||
function main(conf) { | ||
const n = +conf.n; | ||
|
||
switch (conf.method) { | ||
case 'trackingEnabled': | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
new AsyncResource('foobar'); | ||
} | ||
endAfterGC(n); | ||
break; | ||
case 'trackingDisabled': | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
new AsyncResource('foobar', { requireManualDestroy: true }); | ||
} | ||
endAfterGC(n); | ||
break; | ||
default: | ||
throw new Error('Unsupported method'); | ||
} | ||
} |
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
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
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
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,27 @@ | ||
'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))); | ||
}); |
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,21 @@ | ||
'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 async_hooks = require('async_hooks'); | ||
|
||
const hook = async_hooks.createHook({ | ||
destroy: common.mustCall(1) // only 1 immediate is destroyed | ||
}).enable(); | ||
|
||
new async_hooks.AsyncResource('foobar', { requireManualDestroy: true }); | ||
|
||
setImmediate(() => { | ||
global.gc(); | ||
setImmediate(() => { | ||
hook.disable(); | ||
}); | ||
}); |
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,24 @@ | ||
'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 async_hooks = require('async_hooks'); | ||
|
||
const hook = async_hooks.createHook({ | ||
destroy: common.mustCall(2) // 1 immediate + manual destroy | ||
}).enable(); | ||
|
||
{ | ||
const res = new async_hooks.AsyncResource('foobar'); | ||
res.emitDestroy(); | ||
} | ||
|
||
setImmediate(() => { | ||
global.gc(); | ||
setImmediate(() => { | ||
hook.disable(); | ||
}); | ||
}); |