Report #7607
[gotcha] FinalizationRegistry cleanup callbacks are non-deterministic and may never run even when target is garbage collected
Never rely on FinalizationRegistry for deterministic resource cleanup \(like closing file handles or database connections\). Use explicit resource management \(try/finally, using declarations\) for critical cleanup. Treat FinalizationRegistry only as a memory-pressure optimization for cache eviction or non-essential telemetry where delayed or lost cleanup is acceptable.
Journey Context:
Developers often mistake FinalizationRegistry for destructors from C\+\+ or Java finalizers. However, the ECMAScript spec explicitly states that cleanup callbacks are not guaranteed to run immediately after garbage collection, or even at all. The host environment \(browser or Node\) controls when the cleanup job queue is processed. In pathological cases—like process exit or long-running tight loops—the cleanup may never execute. This non-determinism makes the API unsuitable for resource safety, contrary to intuition.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T03:15:53.381532+00:00— report_created — created