Skip to content

Conversation

@mydea
Copy link
Member

@mydea mydea commented Aug 17, 2023

This is a draft based on top of #8813, once that is merged this can be pointed to develop.

This PR changes the behavior when a session is expired to fully stop & restart the replay.
This means we just re-sample based on sample rates and start a completely new session in that case.

Supersedes #8407


// We can't move `_isEnabled` after awaiting a flush, otherwise we can
// enter into an infinite loop when `stop()` is called while flushing.
this._isEnabled = false;
Copy link
Member Author

Choose a reason for hiding this comment

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

I moved this out of the try-catch, as otherwise this lead to weird race conditions. I guess by having this in the try-catch the timing semantics are slightly different. I had a bunch of cases where stop was then called twice at the exact same time, which was fixed by moving this out here 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Ah right, since try/catch inside of an async function is sugar on top of promises.


// Need to set as enabled before we start recording, as `record()` can trigger a flush with a new checkout
this._isEnabled = true;
this._isPaused = false;
Copy link
Member Author

Choose a reason for hiding this comment

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

making sure we are also unpaused here, when starting new.

*/
private async _runFlush(): Promise<void> {
if (!this.session || !this.eventBuffer) {
const replayId = this.getSessionId();
Copy link
Member Author

Choose a reason for hiding this comment

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

capturing the replayId to use at the very top here. With this, if this runs out of sync while processing, we should still never send data to the wrong replay ID, at least.


// if this changed in the meanwhile, e.g. because the session was refreshed or similar, we abort here
if (replayId !== this.getSessionId()) {
return;
Copy link
Member Author

Choose a reason for hiding this comment

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

Just bailing out here, i think that should be fine? If this changed in the meanwhile, if we flush again later we should have discarded all the stuff before already...?

Copy link
Member

Choose a reason for hiding this comment

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

There may be an edgecase that we will have to deal with separately where we have an ongoing/suspended flush, and a new session is created w/ checkout snapshot that gets added to buffer and then discarded here

this._maybeSaveSession();

// Note this empties the event buffer regardless of outcome of sending replay
const recordingData = await this.eventBuffer.finish();
Copy link
Member Author

Choose a reason for hiding this comment

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

Moving this down here, the idea is:

  1. We capture the timestamp, event context etc. first
  2. Then, if the buffer flushing takes a long time, we don't care, even if in the meanwhile the session was refreshed we can still send the stuff to the old session. Also timestamps should be less prone to be too late due to buffer flushing, and should actually be more correct because it is the time of the flush and not whenever the buffer finished.

* If this is false, the session should not be refreshed when it was inactive.
* This can be the case if you had a buffered session which is now recording because an error happened.
*/
shouldRefresh: boolean;
Copy link
Member Author

Choose a reason for hiding this comment

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

Only now did I notice that we can literally replace this everywhere by just checking segmentId === 0 🤦

@github-actions
Copy link
Contributor

github-actions bot commented Aug 17, 2023

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped) 75.29 KB (-0.1% 🔽)
@sentry/browser (incl. Tracing) - Webpack (gzipped) 31.29 KB (+0.06% 🔺)
@sentry/browser - Webpack (gzipped) 21.89 KB (+0.07% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (gzipped) 70 KB (-0.14% 🔽)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (gzipped) 28.4 KB (-0.07% 🔽)
@sentry/browser - ES6 CDN Bundle (gzipped) 20.47 KB (-0.05% 🔽)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (minified & uncompressed) 221.17 KB (-0.17% 🔽)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (minified & uncompressed) 85.82 KB (+0.01% 🔺)
@sentry/browser - ES6 CDN Bundle (minified & uncompressed) 60.67 KB (+0.02% 🔺)
@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped) 31.28 KB (-0.05% 🔽)
@sentry/react (incl. Tracing, Replay) - Webpack (gzipped) 75.32 KB (-0.1% 🔽)
@sentry/react - Webpack (gzipped) 21.92 KB (+0.07% 🔺)
@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped) 93.18 KB (-0.08% 🔽)
@sentry/nextjs Client - Webpack (gzipped) 50.85 KB (+0.04% 🔺)

Base automatically changed from fn/replay-restart-buffer to develop August 18, 2023 06:12
@mydea mydea force-pushed the fn/isolate-eventBuffer branch from bc72625 to da7f8d3 Compare August 18, 2023 06:17
@mydea mydea marked this pull request as ready for review August 18, 2023 06:17
}

logInfoNextTick('[Replay] Session in sessionStorage is expired, creating new one...');
return createSession(sessionOptions, { previousSessionId: existingSession.id });

Check failure

Code scanning / CodeQL

Insecure randomness

This uses a cryptographically insecure random number generated at [Math.random()](1) in a security context.
* called outside of constructor.
*/
public initializeSampling(): void {
public initializeSampling(previousSessionId?: string): void {

Check failure

Code scanning / CodeQL

Insecure randomness

This uses a cryptographically insecure random number generated at [Math.random()](1) in a security context.
try {
logInfo(
`[Replay] Stopping Replay${reason ? ` triggered by ${reason}` : ''}`,
`[Replay] Stopping Replay${reason ? ` triggered by ${reason}` : ''} ${new Date().toISOString()} ${
Copy link
Member

Choose a reason for hiding this comment

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

Should we keep the date string here?

Copy link
Member Author

Choose a reason for hiding this comment

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

oops, no!


// if this changed in the meanwhile, e.g. because the session was refreshed or similar, we abort here
if (replayId !== this.getSessionId()) {
return;
Copy link
Member

Choose a reason for hiding this comment

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

There may be an edgecase that we will have to deal with separately where we have an ongoing/suspended flush, and a new session is created w/ checkout snapshot that gets added to buffer and then discarded here

@mydea mydea force-pushed the fn/isolate-eventBuffer branch 2 times, most recently from f70f354 to 4b185e9 Compare September 1, 2023 09:15
@mydea mydea force-pushed the fn/isolate-eventBuffer branch from b571601 to 7bc5c80 Compare September 11, 2023 07:37
@mydea mydea merged commit 30c4540 into develop Sep 11, 2023
@mydea mydea deleted the fn/isolate-eventBuffer branch September 11, 2023 08:56
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