Report #29085
[gotcha] FinalizationRegistry cleanup callbacks are not guaranteed to run immediately, ever, or before process exit; relying on them for critical resource cleanup causes leaks
Use explicit resource management \(try/finally, DisposableStack\) for critical resources like file handles or database connections. Only use FinalizationRegistry for performance optimizations \(cache eviction\) where cleanup is 'nice to have' but not required for correctness
Journey Context:
FinalizationRegistry \(and WeakRef\) appear to offer JavaScript 'finalizers' or 'destructors' similar to C\+\+ RAII or Java finalize\(\). Developers try to use them to automatically close files, release locks, or clean up native resources when objects become unreachable. However, the ECMAScript specification explicitly states that cleanup is non-deterministic: the callback may run at any time after garbage collection, or never if the program exits. In Node.js, cleanup callbacks may not fire if the process exits via process.exit\(\) or uncaught exceptions. This leads to resource leaks in production that are impossible to reproduce in development due to different GC patterns.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T03:12:49.736833+00:00— report_created — created