Closed Bug 2045488 Opened 5 months ago Closed 5 months ago

FinalizationRegistry leaks a FinalizationRecordObject per registration even after the cleanup callback has run

Categories

(Core :: JavaScript: GC, defect, P3)

defect

Tracking

()

RESOLVED FIXED
153 Branch
Tracking Status
firefox153 --- fixed

People

(Reporter: jandem, Assigned: jonco)

References

(Blocks 2 open bugs)

Details

(Keywords: ai-involved)

Attachments

(1 external file)

Summary

Every FinalizationRegistry.prototype.register(target, held) permanently leaks ~73 bytes of GC heap for the lifetime of the registry — even after the target has died and its cleanup callback has run. For a long-lived registry (the common pattern: Emscripten / wasm-bindgen-style bindings registering every wrapped handle with one module-level registry), heap growth is unbounded and never reclaimed. 100,000 fully-cleaned registrations retain 7,311,360 bytes (73.1/registration); growth is linear (14.6 MB at 200k). Dropping the registry releases everything.

Root cause

Regression from bug 1986862 (commit 34f6ff62ef05, FF144), "Refactor finalization registries to remove global data". The strong reference keeping a FinalizationRecordObject alive moved from a per-global recordSet into the registry itself (recordsWithoutToken vector + per-token map vectors), but the old pruning was dropped. FinalizationQueueObject::cleanupQueuedRecords (FinalizationRegistryObject.cpp:837) calls record->clear() and pops from the cleanup queue, but never removes the record from the registry's strongly-traced vectors. recordsWithoutToken is only ever shortened by the error-undo popBack (line 479) or registry finalization (line 286) — so a queued-and-cleaned record stays reachable via FinalizationRegistryObject::trace (line 239) until the registry dies.

Two stale comments at HEAD confirm the regression is unintended: FinalizationRegistryObject.h:107-110 still references the deleted shouldRemoveRecord and claims records "are removed when the record is queued for cleanup" (they are not); FinalizationObservers.cpp:348 still mentions the removed global recordSet.

unregister() is unaffected — it removes the whole map entry (0 bytes retained, verified).

Test case

let cleaned = 0;
let reg = new FinalizationRegistry(() => { cleaned++; });

const N = 100000;
for (let i = 0; i < N; i++) {
  reg.register({}, null);
}

gc();             // targets die; records queued for cleanup
drainJobQueue();  // cleanup callbacks run; records cleared
assertEq(cleaned, N);

gc(); gc();
const withRegistry = gcparam("gcBytes");
reg = null;       // drop the now logically-empty registry
gc(); gc();
const withoutRegistry = gcparam("gcBytes");

const retained = withRegistry - withoutRegistry;
print("retained by logically-empty registry: " + retained + " bytes (" +
      (retained / N).toFixed(1) + " per registration)");
assertEq(retained < N * 32, true);   // fails: ~73 bytes/registration retained

js --fuzzing-safe repro.js → retained by logically-empty registry: 7311360 bytes (73.1 per registration). Also reproduces via the synchronous cleanupSome path and scales linearly (14.6 MB at N=200000).

Expected vs. observed

  • Expected (pre-FF144): after gc() + cleanups complete, the registry holds no live registrations and a subsequent GC returns the heap to baseline.
  • Observed: 73 bytes/registration (the tenured FinalizationRecordObjects) stay reachable; only dropping the registry (or unregister token) releases them.

Suggested fix

Prune cleared/unregistered records from recordsWithoutToken and the per-token vectors — e.g. re-add a shouldRemoveRecord-style filter in FinalizationRegistryObject::traceWeak, or remove the record when cleanupQueuedRecords calls record->clear().

leak -> not a sec bug

Keywords: sec-other
Blocks: GC, sm-security
Severity: -- → S3
Priority: -- → P3

Not security sensitive.

Assignee: nobody → jcoppeard
Group: javascript-core-security
Keywords: sec-other

The problem here is that we never removed the records from the
FinalizationRegistry object even after they were cleaned up. This didn't affect
correctness as they were not longer registered as observing any target.

We can remove the old ones in the traceWeak method. This removes the records
when they get transferred to the cleanup queue as this sets their queue slot to
undefined by calling clear() which causes isRegistered() to return false.

Status: NEW → RESOLVED
Closed: 5 months ago
Resolution: --- → FIXED
Target Milestone: --- → 153 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: