Skip to content

fix(rrweb): Don't throw when tearing down a locked adoptedStyleSheets patch - #351

Merged
gggritso merged 5 commits into
sentry-v2from
fix/adopted-stylesheets-teardown
Nov 10, 2026
Merged

gggritso merged 5 commits into
sentry-v2from
fix/adopted-stylesheets-teardown

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

rrweb patches adoptedStyleSheets on every document so that it can capture dynamically added stylesheets. Setup copied configurable from the current adoptedStyleSheets property, and teardown redefined the property. Teardown throws TypeError: Cannot redefine property: adoptedStyleSheets in two cases:

  • the prototype's adoptedStyleSheets property was already non-configurable, so our own property started out locked (maybe an extension caused this), or
  • another script locked our property during recording (a non-configurable redefine, or Object.seal/Object.freeze on document).

When it throws, the rest of teardown is skipped. In the Replay SDK this happens inside sendBufferedReplayOrFlush → stopRecording(), so the buffered replay never restarts in session mode, and we lose everything after the error.

This PR makes a few changes.

Firstly, the adoptedStyleSheets patch is now marked as configurable: true which means it's always safe for us to remove the patch that we ourselves added. There is also a bit of checking to make sure that we are able to set adoptedStyleSheets back to what it was before we tampered with it. Using Reflect. fails quietly instead of erroring out.

Secondly, the teardown process is now a bit safer to run. If there's an exception thrown during one of the steps, the other steps still run.

I think it might be good for us to add some telemetry around this, but I'm not confident what it would be yet, so I left it out. I also think this might be a worthy upstream into rrweb, but I'm not sure about that either.

Closes REPLAY-1019

--

View Session [Sentry]

… patch

rrweb patches `adoptedStyleSheets` on each document and shadow root it
records, copying `configurable` from the prototype. If the prototype's
property was non-configurable, or another script locked our property
(non-configurable redefine, `Object.seal`/`freeze`) during recording,
cleanup threw `TypeError: Cannot redefine property: adoptedStyleSheets`.
That aborted the rest of teardown and, in the Replay SDK, stopped the
buffer-to-session restart.

- Always install the patch as configurable, and remove it with
  `Reflect.deleteProperty`/`Reflect.defineProperty` instead of
  redefining it.
- If it can't be removed, leave it behind as a pass-through and reuse it
  on the next recording.
- Skip the patch if the host already has a non-configurable own property.
- Run every teardown step in `record()` and `initObservers()` even if one
  throws, then rethrow the first error.

Refs REPLAY-1019

Co-Authored-By: George Gritsouk <[email protected]>
@linear-code

linear-code Bot commented Oct 30, 2026

Copy link
Copy Markdown

REPLAY-1019

@gggritso gggritso left a comment

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.

Overall this makes sense but I have a lot of questions about the behaviour

Comment thread packages/rrweb/src/utils.ts Resolved
Comment thread packages/rrweb/src/record/observer.ts Resolved
Comment thread packages/rrweb/src/record/observer.ts Resolved
Comment thread packages/rrweb/src/record/observer.ts Resolved
Comment thread packages/rrweb/src/record/observer.ts Resolved
Comment thread packages/rrweb/src/record/observer.ts Resolved
Comment thread packages/rrweb/src/record/observer.ts Resolved
Drop the per-host patch registry. Each observer keeps its own setter and
a recording flag, and teardown removes the property only if it is still
ours. If another script locked it meanwhile, it stays as a pass-through
and later recordings skip the patch, as they do for any pre-existing lock.

Also skip the patch when the host is sealed or frozen, explain why
configurable: true is safe, rename callAllHandlers to
runAllEvenIfOneThrows, keep the original hostId code, and drop the
changeset, which this fork does not use for releases.

@gggritso gggritso left a comment

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.

Starting to look good, a few more questions though

Comment thread packages/rrweb/src/record/index.ts Resolved
Comment thread packages/rrweb/src/record/observer.ts Resolved
Comment thread packages/rrweb/src/record/observer.ts Resolved
Comment thread packages/rrweb/src/record/observer.ts Resolved
Rename runAllEvenIfOneThrows to runAllAndRethrowFirstError and pass record()'s
final cleanup steps to it instead of wrapping it in try/finally. Rename the
saved own descriptor and the patched setter so teardown's ownership check
reads clearly, and write the configurable comment in the present tense.

Replace the lock-parameterised test helper with one test per scenario that
does its own setup and uses plain assertions. Shared recording helpers are
installed in the page in beforeEach.

@gggritso gggritso left a comment

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.

Looking good, just a few more tweaks needed before we can open this up

Comment thread packages/rrweb/src/record/observer.ts Resolved
Comment thread packages/rrweb/src/record/observer.ts Resolved
Comment thread packages/rrweb/src/record/observer.ts Resolved
Comment thread packages/rrweb/src/record/observer.ts Resolved
Comment thread packages/rrweb/src/utils.ts Resolved
@gggritso gggritso changed the title fix(rrweb): Don't throw when tearing down a locked adoptedStyleSheets patch fix(rrweb): Don't throw when tearing down a locked adoptedStyleSheets patch Nov 6, 2026
…leSheets names

runAll now runs every handler and returns the errors they threw. record()'s
stop function and initObservers()'s cleanup rethrow the first one, so the SDK
still sees the same failure as before.

Rename the adoptedStyleSheets setter and active flag to say what they belong
to, and reword the comments to name where each descriptor comes from and to
say that the ownership check and the Reflect calls are there to be safe.
@gggritso

gggritso commented Nov 6, 2026

Copy link
Copy Markdown
Member

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 4259a1e. Configure here.

@gggritso
gggritso marked this pull request as ready for review November 6, 2026 21:59

@logaretm logaretm left a comment

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.

Looked like #352 to me at a glance, but they are actually two separate bugs.

LGTM!

Comment thread packages/rrweb/src/record/observer.ts Resolved
@gggritso
gggritso merged commit 4b75e32 into sentry-v2 Nov 10, 2026
21 checks passed
@gggritso
gggritso deleted the fix/adopted-stylesheets-teardown branch November 10, 2026 03:46
gggritso added a commit to getsentry/sentry that referenced this pull request Nov 12, 2026
…130930)

Bumps the Sentry JavaScript SDK from 11.7.0 to
[11.12.0](https://github.com/getsentry/sentry-javascript/releases/tag/11.12.0),
mainly to pick up rrweb
[2.46.0](https://github.com/getsentry/rrweb/releases/tag/2.46.0)
(getsentry/sentry-javascript#26241).

rrweb 2.46.0 fixes:
- Clear leftover nodes when `document.open()` is skipped
(getsentry/rrweb#350)
- Don't record the default value of cleared inputs (getsentry/rrweb#349)
- Keep adopted stylesheets on seek when their first host was removed
(getsentry/rrweb#352)
- Don't throw when tearing down a locked `adoptedStyleSheets` patch
(getsentry/rrweb#351)

Changes:
- `@sentry/browser`, `@sentry/core`, `@sentry/node`, `@sentry/react`:
11.7.0 → 11.12.0 across `static/app`, `static/gsApp`, `static/gsAdmin`,
`static/packages/scraps`, and `tests/js`.
- `@sentry/rrweb`, `@sentry/rrweb-player`, `@sentry/rrweb-snapshot`:
2.45.0 → 2.46.0, so the replay player uses the same rrweb as the SDK.
- `@sentry/rrweb-types`: 2.44.0 → 2.46.0. This drops the duplicate
`@sentry/[email protected]` / `@sentry/[email protected]` entries
from the lockfile.

<!-- junior-request-attribution:start -->
via **George Gritsouk**.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AD0AR76A9F5G%3A1794502210.473359 -->

Closes REPLAY-1029
Closes REPLAY-1019

--

[View
Session](https://junior-prod.sentry.dev/conversations/slack%3AD0AR76A9F5G%3A1794502210.473359)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AD0AR76A9F5G%3A1794502210.473359/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: George Gritsouk <[email protected]>
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.

2 participants