Files
node/test/fixtures/process/finalization-cleanup.mjs
T
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

35 lines
774 B
JavaScript

import { deepStrictEqual } from 'assert'
import { setImmediate } from 'timers/promises'
const keptAlive = []
const finalized = []
function onFinalize(obj) {
finalized.push(obj.name)
}
{
const first = { name: 'first' }
let collected = { name: 'collected' }
const third = { name: 'third' }
keptAlive.push(first, third)
process.finalization.register(first, onFinalize)
process.finalization.register(collected, onFinalize)
process.finalization.register(third, onFinalize)
collected = null
}
// Give V8 a few chances to collect `collected` and run the
// FinalizationRegistry cleanup before process exit.
for (let i = 0; i < 10; i++) {
gc()
await setImmediate()
}
process.on('exit', function () {
deepStrictEqual(finalized, ['first', 'third'])
})