-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
fix(replay): Fully stop & restart session when it expires #8834
Conversation
@@ -372,15 +370,18 @@ export class ReplayContainer implements ReplayContainerInterface { | |||
return; | |||
} | |||
|
|||
// 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; |
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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.
@@ -735,6 +726,7 @@ export class ReplayContainer implements ReplayContainerInterface { | |||
|
|||
// 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; |
There was a problem hiding this comment.
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.
@@ -1071,7 +1054,9 @@ export class ReplayContainer implements ReplayContainerInterface { | |||
* Should never be called directly, only by `flush` | |||
*/ | |||
private async _runFlush(): Promise<void> { | |||
if (!this.session || !this.eventBuffer) { | |||
const replayId = this.getSessionId(); |
There was a problem hiding this comment.
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.
@@ -1091,6 +1076,11 @@ export class ReplayContainer implements ReplayContainerInterface { | |||
return; | |||
} | |||
|
|||
// if this changed in the meanwhile, e.g. because the session was refreshed or similar, we abort here | |||
if (replayId !== this.getSessionId()) { | |||
return; |
There was a problem hiding this comment.
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...?
There was a problem hiding this comment.
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
const eventContext = this._popEventContext(); | ||
// Always increment segmentId regardless of outcome of sending replay | ||
const segmentId = this.session.segmentId++; | ||
this._maybeSaveSession(); | ||
|
||
// Note this empties the event buffer regardless of outcome of sending replay | ||
const recordingData = await this.eventBuffer.finish(); |
There was a problem hiding this comment.
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:
- We capture the timestamp, event context etc. first
- 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; |
There was a problem hiding this comment.
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
🤦
size-limit report 📦
|
bc72625
to
da7f8d3
Compare
packages/replay/src/replay.ts
Outdated
try { | ||
logInfo( | ||
`[Replay] Stopping Replay${reason ? ` triggered by ${reason}` : ''}`, | ||
`[Replay] Stopping Replay${reason ? ` triggered by ${reason}` : ''} ${new Date().toISOString()} ${ |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, no!
@@ -1091,6 +1076,11 @@ export class ReplayContainer implements ReplayContainerInterface { | |||
return; | |||
} | |||
|
|||
// if this changed in the meanwhile, e.g. because the session was refreshed or similar, we abort here | |||
if (replayId !== this.getSessionId()) { | |||
return; |
There was a problem hiding this comment.
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
f70f354
to
4b185e9
Compare
b571601
to
7bc5c80
Compare
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