node/test/parallel/test-process-finalization.mjs
Trivikram Kamat 75aec8ecf8
process: fix finalization cleanup ref tracking
Use SafeSet collections for finalization refs so insertion, removal,
and emptiness checks match the identity-based tracking model.

This also fixes cleanup removal for collected refs. Previously cleanup
used the ref index as the splice delete count, which could remove later
live refs when the collected ref was not first.

Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com>
Assisted-by: openai:gpt-5.5
PR-URL: https://github.com/nodejs/node/pull/64087
Fixes: https://github.com/nodejs/node/issues/64086
Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
2026-06-30 21:38:02 +00:00

53 lines
1.5 KiB
JavaScript

import '../common/index.mjs';
import { spawnSyncAndAssert } from '../common/child_process.js';
import fixtures from '../common/fixtures.js';
import { it } from 'node:test';
import assert from 'assert';
const files = [
'close.mjs',
'before-exit.mjs',
'finalization-cleanup.mjs',
'gc-not-close.mjs',
'unregister.mjs',
'different-registry-per-thread.mjs',
];
for (const file of files) {
it(`should exit file ${file} with code=0`, () => {
spawnSyncAndAssert(process.execPath, ['--expose-gc', `${file}`], {
cwd: fixtures.path('process'),
}, {
code: 0,
});
});
}
it('register is different per thread', () => {
spawnSyncAndAssert(process.execPath, ['--expose-gc', 'different-registry-per-thread.mjs'], {
cwd: fixtures.path('process'),
}, {
code: 0,
stdout: 'shutdown on worker\nshutdown on main thread\n',
});
});
it('should throw when register undefined value', () => {
try {
process.finalization.register(undefined);
assert.fail('Expected an error to be thrown for registerFreeOnExit');
} catch (e) {
assert.ok(e.message.includes('must be of type object'), `Expected error message to include 'Invalid' but got: ${e.message}`);
}
try {
process.finalization.registerBeforeExit(undefined);
assert.fail('Expected an error to be thrown for registerFreeOnBeforeExit');
} catch (e) {
assert.ok(e.message.includes('must be of type object'), `Expected error message to include 'Invalid' but got: ${e.message}`);
}
});