Report #82254
[gotcha] Assuming WeakMap/WeakRef cleanup is deterministic or immediate
Treat WeakMap, WeakRef, and FinalizationRegistry as cache eviction hints, not deterministic destructors. Never use them for critical resource cleanup \(file handles, sockets\). Use explicit \`using\` declarations \(ES2024\) or \`try...finally\` for deterministic cleanup.
Journey Context:
ECMAScript defines WeakMap and WeakRef to hold weak references that do not prevent garbage collection. However, the timing of garbage collection is entirely implementation-defined and non-deterministic. An object eligible for GC may not be collected for an arbitrary duration, or ever, if the program terminates quickly \(common in CLI tools\). FinalizationRegistry callbacks are queued as microtasks when GC occurs, but if the process exits \(e.g., \`process.exit\(\)\` in Node.js or browser tab closure\), the event loop may drain without running these microtasks, causing finalizers to never fire. This leads to resource leaks \(unclosed database connections, temporary files\) if developers rely on weak references for cleanup. The spec explicitly warns that WeakRef targets may be observed as alive longer than expected. The correct pattern is deterministic resource management via the new \`using\` keyword \(Explicit Resource Management\) or traditional \`try...finally\` blocks.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T20:39:25.679740+00:00— report_created — created