-
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.
n-api: do not require JS Context for
napi_async_destroy()
Allow the function to be called during GC, which is a common use case. Fixes: #27218 PR-URL: #27255 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
- Loading branch information
Showing
3 changed files
with
78 additions
and
2 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
44 changes: 44 additions & 0 deletions
44
test/node-api/test_make_callback/test-async-hooks-gcable.js
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,44 @@ | ||
'use strict'; | ||
// Flags: --gc-interval=100 --gc-global | ||
|
||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
const { createAsyncResource } = require(`./build/${common.buildType}/binding`); | ||
|
||
// Test for https://github.com/nodejs/node/issues/27218: | ||
// napi_async_destroy() can be called during a regular garbage collection run. | ||
|
||
const hook_result = { | ||
id: null, | ||
init_called: false, | ||
destroy_called: false, | ||
}; | ||
|
||
const test_hook = async_hooks.createHook({ | ||
init: (id, type) => { | ||
if (type === 'test_gcable') { | ||
hook_result.id = id; | ||
hook_result.init_called = true; | ||
} | ||
}, | ||
destroy: (id) => { | ||
if (id === hook_result.id) hook_result.destroy_called = true; | ||
}, | ||
}); | ||
|
||
test_hook.enable(); | ||
createAsyncResource(); | ||
|
||
// Trigger GC. This does *not* use global.gc(), because what we want to verify | ||
// is that `napi_async_destroy()` can be called when there is no JS context | ||
// on the stack at the time of GC. | ||
// Currently, using --gc-interval=100 + 1M elements seems to work fine for this. | ||
const arr = new Array(1024 * 1024); | ||
for (let i = 0; i < arr.length; i++) | ||
arr[i] = {}; | ||
|
||
assert.strictEqual(hook_result.destroy_called, false); | ||
setImmediate(() => { | ||
assert.strictEqual(hook_result.destroy_called, true); | ||
}); |