Skip to content

fix(replay): Fix re-sampled sessions after a click #17008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

billyvg
Copy link
Member

@billyvg billyvg commented Jul 15, 2025

This fixes a bug where an expired session-based replay will always force a new session-based replay after a click, regardless of the actual recording mode.

What is happening is:

  • a session-based replay is left idle
  • user comes back and clicks on the page and triggers the click listener
  • addBreadcrumbEvent() is called which then calls triggerUserActivity() because it is a click
  • next, _checkSession() and _refreshSession() are called and this is where the problem starts

Inside of _refreshSession we stop the current replay (because the session is expired), however stop() is async and is await-ed before we re-sample. So the current replay state while stop() is finishing has:

  • recordingMode = session (initial value)
  • isEnabled = false

Another however, addBreadcrumbEvent (and everything called until _refreshSession) are not async and does wait for resampling (initializeSampling()) to occur. This means that the click breadcrumb ends up causing a flush and always starting a new replay recording because we only check that [recordingMode is buffer](https://github.com/getsentry/sentry-javascript/blob/develop/packages/replay-internal/src/replay.ts#L626).

Solution

When we call stop(), reset the recordingMode to buffer (this should be safe default as it is more restrictive behaviorally than session) and in addUpdate, add a check to see if isEnabled() is true (recording is enabled).

This fixes a bug where an expired session-based replay will always force a new session-based replay after a click, regardless of the actual recording mode.

What is happening is:

- a session-based replay is left idle
- user comes back and clicks on the page and triggers the click listener
- `addBreadcrumbEvent()` is called which then calls `triggerUserActivity()` because it is a click
- next, `_checkSession()` and `_refreshSession()` are called and this is where the problem starts

Inside of `_refreshSession` we stop the current replay (because the session is expired), however `stop()` is async and is `await`-ed before we re-sample. So the current replay state while `stop()` is finishing has:

- `recordingMode` = `session` (initial value)
- `isEnabled` = false

Another however, `addBreadcrumbEvent` (and everything called until `_refreshSession`) are not async and does wait for resampling (`initializeSampling()`) to occur. This means that the click breadcrumb ends up causing a flush and always starting a new replay recording.
Copy link
Contributor

github-actions bot commented Jul 15, 2025

size-limit report 📦

⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Path Size % Change Change
@sentry/browser 23.88 kB - -
@sentry/browser - with treeshaking flags 22.35 kB - -
@sentry/browser (incl. Tracing) 39.75 kB - -
@sentry/browser (incl. Tracing, Replay) 77.89 kB +0.02% +11 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 67.62 kB +0.02% +10 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 82.59 kB +0.01% +8 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 94.69 kB +0.02% +10 B 🔺
@sentry/browser (incl. Feedback) 40.58 kB - -
@sentry/browser (incl. sendFeedback) 28.56 kB - -
@sentry/browser (incl. FeedbackAsync) 33.46 kB - -
@sentry/react 25.61 kB - -
@sentry/react (incl. Tracing) 41.72 kB - -
@sentry/vue 28.31 kB - -
@sentry/vue (incl. Tracing) 41.53 kB - -
@sentry/svelte 23.9 kB - -
CDN Bundle 25.18 kB - -
CDN Bundle (incl. Tracing) 39.44 kB - -
CDN Bundle (incl. Tracing, Replay) 75.45 kB +0.02% +9 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 80.92 kB +0.02% +10 B 🔺
CDN Bundle - uncompressed 73.54 kB - -
CDN Bundle (incl. Tracing) - uncompressed 116.99 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 231.25 kB +0.03% +66 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 244.06 kB +0.03% +66 B 🔺
@sentry/nextjs (client) 43.75 kB - -
@sentry/sveltekit (client) 40.2 kB - -
@sentry/node 167.93 kB -0.01% -1 B 🔽
@sentry/node - without tracing 100.34 kB - -
@sentry/aws-serverless 128.48 kB -0.01% -1 B 🔽

View base workflow run

@billyvg billyvg marked this pull request as ready for review July 15, 2025 02:23
@billyvg billyvg requested a review from a team as a code owner July 15, 2025 02:23
@billyvg billyvg requested a review from chargome July 15, 2025 02:23
@@ -503,6 +503,10 @@ export class ReplayContainer implements ReplayContainerInterface {
// enter into an infinite loop when `stop()` is called while flushing.
this._isEnabled = false;

// Make sure to reset `recordingMode` to `buffer` to avoid any additional
// breadcrumbs to trigger a flush (e.g. in `addUpdate()`)
this.recordingMode = 'buffer';
Copy link
Member

Choose a reason for hiding this comment

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

not sure, do we (need to?) reset this at some point? I guess not, as ended usually means ended for good for us I suppose...?

Copy link
Member Author

Choose a reason for hiding this comment

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

We currently don't need to, but I think it's a safer option to have it reset here.

@billyvg billyvg requested a review from mydea July 15, 2025 14:18
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