Repository navigation
fix(replay): Keep the replay timeline anchored across wall-clock jumps #6542
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
base: main
Are you sure you want to change the base?
Changes from all commits
611cf03
d93efe8
b9aa06f
2d151a4
6ed91a1
540dd60
5259890
eb8b9cd
6d4b6f8
1db10e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package io.sentry.android.replay.capture | ||
|
|
||
| import io.sentry.time.AnchoredClock | ||
| import io.sentry.time.EpochClock | ||
| import io.sentry.time.MonotonicTicker | ||
| import io.sentry.time.Timestamp | ||
| import java.util.concurrent.TimeUnit.NANOSECONDS | ||
| import kotlin.math.abs | ||
|
|
||
| /** | ||
| * The timeline a recording stamps its frames, segments and touch events on. | ||
| * | ||
| * Those stamps are subtracted from each other: a segment is the frames within 5s of its start, and | ||
| * the buffer is the last 30s. So they come from one [AnchoredClock], where a wall-clock change | ||
| * can't drop frames or freeze the video. Breadcrumbs, errors and spans, though, are stamped from | ||
| * the wall clock elsewhere in the SDK, and the player places them on the video by that stamp. So | ||
| * the timeline must not drift away from the wall clock either: when [wallClockJumpMillis] reports a | ||
| * jump, the recording drops what it recorded on the current timeline and [restart]s on the wall | ||
| * clock. | ||
| * | ||
| * Reads are safe from any thread. Checking for a jump and then restarting is a check-then-act, so | ||
| * [BaseCaptureStrategy] does both under its timeline lock. | ||
| */ | ||
| internal class ReplayClock( | ||
| private val epochClock: EpochClock, | ||
| private val ticker: MonotonicTicker, | ||
| continued: AnchoredClock? = null, | ||
| ) { | ||
| internal companion object { | ||
| // just under the 1.1s the player shows as a gap in the video | ||
| const val JUMP_TOLERANCE_MS = 1000L | ||
| } | ||
|
|
||
| /** The current timeline. [restart] replaces it, so a reference read once stays consistent. */ | ||
| @Volatile | ||
| var timeline: AnchoredClock = continued ?: AnchoredClock.create(epochClock, ticker) | ||
| private set | ||
|
|
||
| fun nowMillis(): Long = timeline.nowMillis() | ||
|
|
||
| /** Anchors a new timeline on the wall clock and returns it. */ | ||
| fun restart(): AnchoredClock { | ||
| val next = AnchoredClock.create(epochClock, ticker) | ||
| timeline = next | ||
| return next | ||
| } | ||
|
|
||
| /** | ||
| * How far the wall clock has jumped away from the timeline, positive when it moved forward, or | ||
| * `0` while the two agree within [JUMP_TOLERANCE_MS]. | ||
| */ | ||
| fun wallClockJumpMillis(): Long { | ||
| val jump = epochClock.now().epochMillis - nowMillis() | ||
| return if (abs(jump) > JUMP_TOLERANCE_MS) jump else 0 | ||
| } | ||
| } | ||
|
|
||
| internal val Timestamp.epochMillis: Long | ||
| get() = NANOSECONDS.toMillis(epochNanos()) | ||
|
|
||
| internal fun AnchoredClock.nowMillis(): Long = now().epochMillis |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package io.sentry.android.replay.capture | ||
|
|
||
| import android.graphics.Bitmap | ||
| import io.sentry.DateUtils | ||
| import io.sentry.IScopes | ||
| import io.sentry.SentryLevel.DEBUG | ||
| import io.sentry.SentryLevel.INFO | ||
|
|
@@ -11,6 +12,7 @@ import io.sentry.android.replay.ScreenshotRecorderConfig | |
| import io.sentry.android.replay.capture.CaptureStrategy.ReplaySegment | ||
| import io.sentry.android.replay.util.ReplayRunnable | ||
| import io.sentry.protocol.SentryId | ||
| import io.sentry.time.AnchoredClock | ||
| import io.sentry.time.MonotonicTicker | ||
| import io.sentry.transport.ICurrentDateProvider | ||
| import java.util.Date | ||
|
|
@@ -33,11 +35,12 @@ internal class SessionCaptureStrategy | |
| constructor( | ||
| private val options: SentryOptions, | ||
| private val scopes: IScopes?, | ||
| private val dateProvider: ICurrentDateProvider, | ||
| dateProvider: ICurrentDateProvider, | ||
| ticker: MonotonicTicker, | ||
| executor: ScheduledExecutorService, | ||
| persistingExecutor: ScheduledExecutorService, | ||
| replayCacheProvider: ((replayId: SentryId) -> ReplayCache)? = null, | ||
| continuedTimeline: AnchoredClock? = null, | ||
| ) : | ||
| BaseCaptureStrategy( | ||
| options, | ||
|
|
@@ -47,6 +50,7 @@ constructor( | |
| executor, | ||
| persistingExecutor, | ||
| replayCacheProvider, | ||
| continuedTimeline, | ||
| ) { | ||
| internal companion object { | ||
| private const val TAG = "SessionCaptureStrategy" | ||
|
|
@@ -114,10 +118,20 @@ constructor( | |
| // have to do it before submitting, otherwise if the queue is busy, the timestamp won't be | ||
| // reflecting the exact time of when it was captured | ||
| val currentConfig = recorderConfig | ||
| val frameTimestamp = dateProvider.currentTimeMillis | ||
| val deadlineExceeded = replayDeadline?.hasPassed() == true | ||
| val frame = stampFrame(maxForwardJumpMillis = options.sessionReplay.sessionDuration) | ||
| val frameTimestamp = frame.timestamp | ||
| // a recording can't span a forward jump longer than its cap, so one ends it like the cap does | ||
| val deadlineExceeded = replayDeadline?.hasPassed() == true || frame.jumpedTooFar | ||
| replayExecutor.submit( | ||
| ReplayRunnable("$TAG.add_frame") { | ||
| if (frame.jumped) { | ||
| // first, so this frame isn't dropped with the old timeline's | ||
| dropOldTimeline() | ||
| // a stopped recording has none, and must stay stopped | ||
| if (segmentTimestamp != null) { | ||
| segmentTimestamp = DateUtils.getDateTime(frameTimestamp) | ||
| } | ||
| } | ||
| cache?.store(frameTimestamp) | ||
|
|
||
| val currentSegmentTimestamp = segmentTimestamp | ||
|
|
@@ -198,7 +212,7 @@ constructor( | |
| return | ||
| } | ||
|
|
||
| val requestedAt = dateProvider.currentTimeMillis | ||
| val requestedAt = clock.nowMillis() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Segment request racing a jump mixes timelinesMedium Severity
Additional Locations (1)Reviewed by Cursor Bugbot for commit 1db10e9. Configure here. |
||
| val replayId = currentReplayId | ||
| replayExecutor.submit( | ||
| ReplayRunnable("$TAG.$taskName") { | ||
|
|
||


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.
Touches after a jump cleared by delayed drop
Low Severity
dropOldTimelineclears the sharedcurrentEventsinside the jump frame's executor task, which can run well afterstampFramerestarts the clock. Touches recorded in that window are already stamped on the new timeline. They get cleared along with the old ones and silently disappear from the replay.Additional Locations (1)
sentry-android-replay/src/main/java/io/sentry/android/replay/capture/SessionCaptureStrategy.kt#L126-L134Reviewed by Cursor Bugbot for commit 1db10e9. Configure here.