Skip to content

Fix memory leak in the promise rejection tracker - #2336

Open
tsapeta wants to merge 1 commit into
meta:static_hfrom
tsapeta:tsapeta/fix-promise-rejection-tracker-leak
Open

tsapeta wants to merge 1 commit into
meta:static_hfrom
tsapeta:tsapeta/fix-promise-rejection-tracker-leak

Conversation

@tsapeta

@tsapeta tsapeta commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The promise rejection tracker (enable() in lib/InternalJavaScript/01-Promise.js) keeps the promise and its reason alive forever for each rejection that never gets a handler. The root cause is that it stores a record for each rejection in its rejections table, and only the "handled" hook removes an entry. This happens for reported rejections, and also for rejections that the whitelist does not report. React Native enables the tracker with allRejections: true, so every unhandled rejection stays in memory for the life of the runtime.

  • The record now lives on the promise (_E). The timer holds it until it fires. After that, only the promise holds it, so the GC frees both together. A reported rejection that gets a handler later still calls onHandled.
  • Each enable() call has its own tracker. The "handled" hook ignores records of other trackers, and a timer of an earlier tracker does nothing. This also fixes a bug when enable() runs again while a rejection is pending. The ids started again at 0, so a handler on an old promise cancelled the timer of an unrelated new rejection, and nothing reported it. The old timer also still reported the old promise after it got a handler.

Test Plan

  • New test/hermes/promise-rejection-tracking.js uses WeakRef and gc() to check that a reported rejection and a filtered rejection are collected, and that a second enable() reports only its own rejections. On static_h it fails: both reasons stay alive, the old tracker reports the handled promise, and nothing reports the new rejection. With this change it passes.
  • The lit tests that match promise|async|await|hermes-internal|microtask, which include every test that uses the tracker: 50 passed, 2 expected failures.
  • Heap after 100,000 reported rejections and gc(), measured with the script below (ASan Debug build). Without the tracker, the same loop leaves 230 KiB.
Before After Change
js_allocatedBytes 42,879 KiB 231 KiB -42,648 KiB
var N = 100000, reported = 0;
HermesInternal.enablePromiseRejectionTracker({
  allRejections: true,
  onUnhandled: function () {
    if (++reported === N) setTimeout(measure, 0);
  },
});
function measure() {
  gc();
  var bytes = HermesInternal.getInstrumentedStats().js_allocatedBytes;
  print(Math.round(bytes / 1024) + ' KiB');
}
for (var i = 0; i < N; i++) Promise.reject(new Error('e' + i));

@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Nov 21, 2026
The rejection tracker in 01-Promise.js stores a record for each
rejection that has no handler in its `rejections` table. Only the
"handled" hook removes an entry. So when no handler is ever added, the
tracker keeps the promise and its reason alive forever. This applies to
a reported rejection, and also to a rejection that the whitelist does not
report. React Native enables the tracker with `allRejections: true`, so
an app keeps every unhandled rejection that it reports.

Keep the record on the promise (`_E`) instead. The timer holds the record
until it fires. After that, only the promise holds it, so the GC frees
both together. A reported rejection that gets a handler later still calls
`onHandled`, because the record is on the promise.

A promise can now hold a record of an earlier `enable()` call, so give
each call its own tracker. The "handled" hook ignores records of other
trackers, and a timer of an earlier tracker does nothing. This also fixes
a bug when `enable()` runs again while a rejection is pending. The ids
started again at 0, so a handler on an old promise cancelled the timer of
a new, unrelated rejection, and nothing reported it. Also, the old timer
still reported the old promise after it got a handler.
@tsapeta
tsapeta force-pushed the tsapeta/fix-promise-rejection-tracker-leak branch from 4173a75 to a73985a Compare November 21, 2026 22:06
@tsapeta
tsapeta marked this pull request as ready for review November 21, 2026 22:13
@tsapeta
tsapeta deployed to npm-publish November 21, 2026 22:31 — with GitHub Actions Active

This branch was successfully deployed

1 active deployment
npm-publish — a73985a6 Deployed Nov 21, 2026 by tsapeta via publish #1794
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant