mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
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>
35 lines
774 B
JavaScript
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'])
|
|
})
|