Skip to content

fix(ember): Stop retaining component render payloads in beforeEntries - #23052

Merged
s1gr1d merged 3 commits into
getsentry:developfrom
yashschandra:fix/ember-render-instrumentation-leak
Aug 11, 2026
Merged

fix(ember): Stop retaining component render payloads in beforeEntries#23052
s1gr1d merged 3 commits into
getsentry:developfrom
yashschandra:fix/ember-render-instrumentation-leak

Conversation

@yashschandra

@yashschandra yashschandra commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Before submitting a pull request, please take a look at our
Contributing guidelines and verify:

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).
  • Link an issue if there is one related to your pull request. If no issue is linked, one will be auto-generated and linked.

Problem

_instrumentComponents subscribes to Ember's render.component (and optionally render.getComponentDefinition) instrumentation and stores { payload, now } into a module-scope beforeEntries map keyed by payload.object (a per-component-instance GUID). processComponentRenderAfter reads the entry but never removes it, so every instrumented component render is retained for the lifetime of the page.

Since the render payload references the component instance (view), this pins each destroyed component and everything it references. In our Ember app (Customer.io Journeys) this retained every destroyed message-composer graph when users switched between wizard steps — ~34 MB of unreclaimable heap per step switch, measured in Chrome with --js-flags=--expose-gc and confirmed via heap-snapshot retainer paths ending at the instrumentation subscriber's beforeEntries closure:

(GC roots) → module context → subscribers → before closure → beforeEntries
  → entry → payload → view (component) → parent component graph (~34 MB / cycle)

Fix

  • Convert RenderEntries to a WeakMap keyed on the render payload object itself (per review feedback). This guarantees an entry can never outlive its payload, even if a render's after hook is never called. Ember's @ember/instrumentation passes the same payload object to a subscriber's before and after hooks, so identity-based lookup is safe; payload.object (a string) can't key a WeakMap, so the entry no longer needs to store the payload at all.
  • Still delete the entry in processComponentRenderAfter once the render completes, so a long-lived payload doesn't hold a stale entry between renders.
  • Export _processComponentRenderBefore / _processComponentRenderAfter (following the existing _-prefixed pattern used by instrumentEmberAppInstanceForPerformance) and add unit tests covering: entry removal on completion (both below and above minimumComponentRenderDuration), and that an after without a matching before leaves other in-flight entries untouched.

Verified in the affected app: with this change all destroyed component graphs become collectable and back-to-back heap snapshots across step-switch cycles show only ~0.5 MB/cycle of JIT-code growth instead of ~34 MB/cycle of retained objects.

🤖 Generated with Claude Code

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@yashschandra
yashschandra requested a review from a team as a code owner August 5, 2026 10:22
@yashschandra
yashschandra requested review from nicohrubec and s1gr1d and removed request for a team August 5, 2026 10:22
@github-actions

Copy link
Copy Markdown
Contributor

👋 @nicohrubec, @s1gr1d — Please review this PR when you get a chance!

@s1gr1d
s1gr1d requested a review from mydea August 10, 2026 12:19
now: timestampInSeconds(),
};
beforeEntries[payload.object] = info;
beforeEntries.set(payload.object, info);

@s1gr1d s1gr1d Aug 10, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L: Maybe it would make sense here to use a WeakMap and use the payload as the key. We don't need the info, when the payload is already garbage collected, right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I think moving this to a weakmap is likely the easiest solution here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I think it's still ok to remove the entries as well, but a weak map ensures nothing can linger around if something is not called etc)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Per review on getsentry#23052, switch RenderEntries from Map<string, RenderEntry>
to WeakMap<Payload, RenderEntry>. Keying weakly on the payload object
guarantees nothing lingers even if a render's `after` hook is never
called; the prompt delete is kept so a long-lived payload doesn't hold a
stale entry between renders. Ember passes the same payload object to a
subscriber's before/after hooks, so identity-based lookup is safe.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@yashschandra
yashschandra requested a review from s1gr1d August 10, 2026 15:01
@s1gr1d
s1gr1d enabled auto-merge (squash) August 11, 2026 07:34
@s1gr1d
s1gr1d merged commit 16deb06 into getsentry:develop Aug 11, 2026
247 checks passed
mydea pushed a commit that referenced this pull request Aug 11, 2026
This PR adds the external contributor to the CHANGELOG.md file, so that
they are credited for their contribution. See #23052

Co-authored-by: s1gr1d <32902192+s1gr1d@users.noreply.github.com>
JPeer264 pushed a commit that referenced this pull request Aug 12, 2026
…#23052)

Before submitting a pull request, please take a look at our

[Contributing](https://github.com/getsentry/sentry-javascript/blob/master/CONTRIBUTING.md)
guidelines and verify:

- [x] If you've added code that should be tested, please add tests.
- [x] Ensure your code lints and the test suite passes (`yarn lint`) &
(`yarn test`).
- [x] Link an issue if there is one related to your pull request. If no
issue is linked, one will be auto-generated and linked.

## Problem

`_instrumentComponents` subscribes to Ember's `render.component` (and
optionally `render.getComponentDefinition`) instrumentation and stores
`{ payload, now }` into a module-scope `beforeEntries` map keyed by
`payload.object` (a per-component-instance GUID).
`processComponentRenderAfter` reads the entry but never removes it, so
every instrumented component render is retained for the lifetime of the
page.

Since the render `payload` references the component instance (`view`),
this pins each destroyed component **and everything it references**. In
our Ember app (Customer.io Journeys) this retained every destroyed
message-composer graph when users switched between wizard steps — **~34
MB of unreclaimable heap per step switch**, measured in Chrome with
`--js-flags=--expose-gc` and confirmed via heap-snapshot retainer paths
ending at the instrumentation subscriber's `beforeEntries` closure:

```
(GC roots) → module context → subscribers → before closure → beforeEntries
  → entry → payload → view (component) → parent component graph (~34 MB / cycle)
```

## Fix

- Convert `RenderEntries` to a `WeakMap` keyed on the render `payload`
object itself (per review feedback). This guarantees an entry can never
outlive its payload, even if a render's `after` hook is never called.
Ember's `@ember/instrumentation` passes the same payload object to a
subscriber's `before` and `after` hooks, so identity-based lookup is
safe; `payload.object` (a string) can't key a `WeakMap`, so the entry no
longer needs to store the payload at all.
- Still delete the entry in `processComponentRenderAfter` once the
render completes, so a long-lived payload doesn't hold a stale entry
between renders.
- Export `_processComponentRenderBefore` /
`_processComponentRenderAfter` (following the existing `_`-prefixed
pattern used by `instrumentEmberAppInstanceForPerformance`) and add unit
tests covering: entry removal on completion (both below and above
`minimumComponentRenderDuration`), and that an `after` without a
matching `before` leaves other in-flight entries untouched.

Verified in the affected app: with this change all destroyed component
graphs become collectable and back-to-back heap snapshots across
step-switch cycles show only ~0.5 MB/cycle of JIT-code growth instead of
~34 MB/cycle of retained objects.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Yash Suresh Chandra <yashschandra@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
JPeer264 pushed a commit that referenced this pull request Aug 12, 2026
This PR adds the external contributor to the CHANGELOG.md file, so that
they are credited for their contribution. See #23052

Co-authored-by: s1gr1d <32902192+s1gr1d@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants