1'use strict'; 2// Flags: --gc-interval=100 --gc-global 3 4const common = require('../../common'); 5const assert = require('assert'); 6const async_hooks = require('async_hooks'); 7const { 8 createAsyncResource, 9 destroyAsyncResource, 10 makeCallback, 11} = require(`./build/${common.buildType}/binding`); 12 13// Test for https://github.com/nodejs/node/issues/27218: 14// napi_async_destroy() can be called during a regular garbage collection run. 15 16const hook_result = { 17 id: null, 18 init_called: false, 19 destroy_called: false, 20}; 21 22const test_hook = async_hooks.createHook({ 23 init: (id, type) => { 24 if (type === 'test_async') { 25 hook_result.id = id; 26 hook_result.init_called = true; 27 } 28 }, 29 destroy: (id) => { 30 if (id === hook_result.id) hook_result.destroy_called = true; 31 }, 32}); 33 34test_hook.enable(); 35const asyncResource = createAsyncResource( 36 { foo: 'bar' }, 37 /* destroy_on_finalizer */false 38); 39 40// Trigger GC. This does *not* use global.gc(), because what we want to verify 41// is that `napi_async_destroy()` can be called when there is no JS context 42// on the stack at the time of GC. 43// Currently, using --gc-interval=100 + 1M elements seems to work fine for this. 44const arr = new Array(1024 * 1024); 45for (let i = 0; i < arr.length; i++) 46 arr[i] = {}; 47 48assert.strictEqual(hook_result.destroy_called, false); 49setImmediate(() => { 50 assert.strictEqual(hook_result.destroy_called, false); 51 makeCallback(asyncResource, process, () => { 52 const executionAsyncResource = async_hooks.executionAsyncResource(); 53 // Assuming the executionAsyncResource was created for the absence of the 54 // initial `{ foo: 'bar' }`. 55 // This is the worst path of `napi_async_context` related API of 56 // recovering from the condition and not break the executionAsyncResource 57 // shape, although the executionAsyncResource might not be correct. 58 assert.strictEqual(typeof executionAsyncResource, 'object'); 59 assert.strictEqual(executionAsyncResource.foo, undefined); 60 destroyAsyncResource(asyncResource); 61 setImmediate(() => { 62 assert.strictEqual(hook_result.destroy_called, true); 63 }); 64 }); 65}); 66