Report #7315
[gotcha] FinalizationRegistry cleanup callbacks do not run deterministically or immediately after garbage collection
Never use FinalizationRegistry for deterministic resource cleanup \(file handles, locks, sockets\); implement explicit close/dispose methods and use try/finally blocks; treat cleanup callbacks as best-effort memory pressure hints only
Journey Context:
Developers familiar with RAII patterns from C\+\+ or Python's \`\_\_del\_\_\` expect deterministic destruction. JavaScript's garbage collection is non-deterministic, and while \`FinalizationRegistry\` allows registering cleanup callbacks, the ECMAScript spec explicitly allows engines to delay cleanup indefinitely or skip it entirely \(e.g., if the program exits or during low memory pressure\). Relying on these callbacks to close files, release locks, or clear intervals leads to resource leaks in production that appear to 'work' in development due to different GC pressure. The only safe pattern is explicit resource management \(try/finally or the \`using\` syntax proposal\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T02:20:23.798417+00:00— report_created — created