process: fix finalization cleanup ref tracking · nodejs/node@bdf3262 · GitHub
Skip to content

Commit bdf3262

Browse files
trivikrrichardlau
authored andcommitted
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: #64087 Fixes: #64086 Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
1 parent 9e50bb0 commit bdf3262

3 files changed

Lines changed: 51 additions & 17 deletions

File tree

lib/internal/process/finalization.js

Lines changed: 16 additions & 17 deletions
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { deepStrictEqual } from 'assert'
2+
import { setImmediate } from 'timers/promises'
3+
4+
const keptAlive = []
5+
const finalized = []
6+
7+
function onFinalize(obj) {
8+
finalized.push(obj.name)
9+
}
10+
11+
{
12+
const first = { name: 'first' }
13+
let collected = { name: 'collected' }
14+
const third = { name: 'third' }
15+
16+
keptAlive.push(first, third)
17+
18+
process.finalization.register(first, onFinalize)
19+
process.finalization.register(collected, onFinalize)
20+
process.finalization.register(third, onFinalize)
21+
22+
collected = null
23+
}
24+
25+
// Give V8 a few chances to collect `collected` and run the
26+
// FinalizationRegistry cleanup before process exit.
27+
for (let i = 0; i < 10; i++) {
28+
gc()
29+
await setImmediate()
30+
}
31+
32+
process.on('exit', function () {
33+
deepStrictEqual(finalized, ['first', 'third'])
34+
})

test/parallel/test-process-finalization.mjs

Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)