Files
node/test/parallel/test-process-finalization.mjs
Matteo Collina 9a9f7e87b8 process: return unregister fn from finalization
process.finalization.register() and registerBeforeExit() now return
a function that removes only that registration, leaving other
registrations for the same object in place. The function does not
hold a strong reference to the object.

Each registration now uses its WeakRef as the unregister token, which
also makes process.finalization.unregister() remove the entries from
the FinalizationRegistry; previously it passed the object as the
token, which was never registered, so the call had no effect.

Refs: https://github.com/nodejs/node/pull/66482

Signed-off-by: Matteo Collina <matteo.collina@gmail.com>
Assisted-by: pi
PR-URL: https://github.com/nodejs/node/pull/66482
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
2026-10-18 06:36:43 +00:00

55 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',
'unregister-function.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}`);
}
});