Repository navigation
Conversation
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
force-pushed
the
tsapeta/fix-promise-rejection-tracker-leak
branch
from
November 21, 2026 22:06
4173a75 to
a73985a
Compare
tsapeta
marked this pull request as ready for review
November 21, 2026 22:13
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The promise rejection tracker (
enable()inlib/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 itsrejectionstable, 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 withallRejections: true, so every unhandled rejection stays in memory for the life of the runtime._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 callsonHandled.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 whenenable()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
test/hermes/promise-rejection-tracking.jsusesWeakRefandgc()to check that a reported rejection and a filtered rejection are collected, and that a secondenable()reports only its own rejections. Onstatic_hit fails: both reasons stay alive, the old tracker reports the handled promise, and nothing reports the new rejection. With this change it passes.promise|async|await|hermes-internal|microtask, which include every test that uses the tracker: 50 passed, 2 expected failures.gc(), measured with the script below (ASan Debug build). Without the tracker, the same loop leaves 230 KiB.js_allocatedBytes