Skip to content

Commit 540dd60

Browse files
runningcodeclaude
andcommitted
fix(replay): Keep touch events of segments queued before a wall-clock jump
A frame that followed a jump drained every queued touch event into the segment closing the old timeline at stamp time. A segment task already queued before it, such as a 5s boundary or a pause, then built its recording without its touches, and the closing segment dropped them because they were stamped before its start. Each timeline now has its own touch-event queue. The jump starts a new one for touches, and segments built on the replay executor keep using the old one until the jump's task moves the executor onto the new timeline. Refs JAVA-829 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6ed91a1 commit 540dd60

5 files changed

Lines changed: 56 additions & 20 deletions

File tree

‎sentry-android-replay/detekt-baseline.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<ID>LongMethod:ReplayIntegrationWithRecorderTest.kt$ReplayIntegrationWithRecorderTest$@Test fun `works with different recorder`()</ID>
5959
<ID>LongMethod:SimpleVideoEncoder.kt$SimpleVideoEncoder$private fun drainCodec(endOfStream: Boolean)</ID>
6060
<ID>LongMethod:ViewHierarchyNode.kt$ViewHierarchyNode.Companion$@JvmStatic fun fromView( view: View, parent: ViewHierarchyNode?, distance: Int, options: SentryMaskingOptions, ): ViewHierarchyNode</ID>
61-
<ID>LongParameterList:BaseCaptureStrategy.kt$BaseCaptureStrategy$( duration: Long, currentSegmentTimestamp: Date, replayId: SentryId, segmentId: Int, height: Int, width: Int, frameRate: Int, bitRate: Int, replayType: ReplayType = this.replayType, cache: ReplayCache? = this.cache, screenAtStart: String? = this.screenAtStart, breadcrumbs: List&lt;Breadcrumb>? = null, events: Deque&lt;RRWebEvent> = this.currentEvents, )</ID>
61+
<ID>LongParameterList:BaseCaptureStrategy.kt$BaseCaptureStrategy$( duration: Long, currentSegmentTimestamp: Date, replayId: SentryId, segmentId: Int, height: Int, width: Int, frameRate: Int, bitRate: Int, replayType: ReplayType = this.replayType, cache: ReplayCache? = this.cache, screenAtStart: String? = this.screenAtStart, breadcrumbs: List&lt;Breadcrumb>? = null, events: Deque&lt;RRWebEvent> = this.executorEvents, )</ID>
6262
<ID>LongParameterList:CaptureStrategy.kt$CaptureStrategy.Companion$( options: SentryOptions, video: File, currentReplayId: SentryId, segmentTimestamp: Date, segmentId: Int, height: Int, width: Int, frameCount: Int, frameRate: Int, videoDuration: Long, replayType: ReplayType, screenAtStart: String?, breadcrumbs: List&lt;Breadcrumb>, events: Deque&lt;RRWebEvent>, traceIds: List&lt;String>, segmentNames: List&lt;String>, )</ID>
6363
<ID>LongParameterList:CaptureStrategy.kt$CaptureStrategy.Companion$( scopes: IScopes?, options: SentryOptions, duration: Long, currentSegmentTimestamp: Date, replayId: SentryId, segmentId: Int, height: Int, width: Int, replayType: ReplayType, cache: ReplayCache?, frameRate: Int, bitRate: Int, screenAtStart: String?, breadcrumbs: List&lt;Breadcrumb>?, events: Deque&lt;RRWebEvent>, traceIds: List&lt;String> = emptyList(), segmentNames: List&lt;String> = emptyList(), )</ID>
6464
<ID>LongParameterList:ComposeViewHierarchyNode.kt$ComposeViewHierarchyNode$( node: LayoutNode, parent: ViewHierarchyNode?, distance: Int, isComposeRoot: Boolean, options: SentryMaskingOptions, logger: ILogger, )</ID>

‎sentry-android-replay/src/main/java/io/sentry/android/replay/capture/BaseCaptureStrategy.kt‎

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import io.sentry.time.Timestamp
4242
import io.sentry.transport.ICurrentDateProvider
4343
import io.sentry.util.AutoClosableReentrantLock
4444
import java.io.File
45-
import java.util.ArrayDeque
4645
import java.util.Date
4746
import java.util.Deque
4847
import java.util.concurrent.ConcurrentLinkedDeque
@@ -84,10 +83,9 @@ constructor(
8483

8584
/**
8685
* Makes moving onto a new timeline atomic: checking the wall clock, restarting the clock and
87-
* draining [currentEvents] for the segment that closes the old timeline happen together. Touch
88-
* events are stamped and added under it too. So two frames can't both follow one jump, and every
89-
* touch event stamped on the old timeline is drained while every one stamped on the new one
90-
* stays.
86+
* starting new [currentEvents] happen together. Touch events are stamped and added under it too.
87+
* So two frames can't both follow one jump, and every touch event lands in the events of the
88+
* timeline it was stamped on.
9189
*/
9290
private val timelineLock = AutoClosableReentrantLock()
9391

@@ -96,7 +94,9 @@ constructor(
9694
* is building. Only [stampFrame] restarts the clock, and that frame's task moves this onto the
9795
* new timeline, so the two differ only while such a task is queued.
9896
*/
99-
@Volatile protected var executorTimeline: AnchoredClock = clock.timeline
97+
@Volatile
98+
protected var executorTimeline: AnchoredClock = clock.timeline
99+
private set
100100

101101
private val gestureConverter = ReplayGestureConverter({ clock.nowMillis() }, ticker)
102102

@@ -149,7 +149,16 @@ constructor(
149149
override var isFlushed: Boolean by
150150
persistableAtomic(initialValue = false, propertyName = SEGMENT_KEY_FLUSHED)
151151

152-
protected val currentEvents: Deque<RRWebEvent> = ConcurrentLinkedDeque()
152+
/** The touch events of the timeline [clock] is on, which new ones are added to. */
153+
@Volatile
154+
protected var currentEvents: Deque<RRWebEvent> = ConcurrentLinkedDeque()
155+
private set
156+
157+
/**
158+
* The touch events of [executorTimeline], which segments built on [replayExecutor] take theirs
159+
* from. So a segment queued before a jump still finds the touch events of the old timeline.
160+
*/
161+
@Volatile private var executorEvents: Deque<RRWebEvent> = currentEvents
153162
private val replayContextLock = Any()
154163
private val currentTraceIds: MutableSet<String> = linkedSetOf()
155164
private val currentSegmentNames: MutableSet<String> = linkedSetOf()
@@ -213,7 +222,7 @@ constructor(
213222
cache: ReplayCache? = this.cache,
214223
screenAtStart: String? = this.screenAtStart,
215224
breadcrumbs: List<Breadcrumb>? = null,
216-
events: Deque<RRWebEvent> = this.currentEvents,
225+
events: Deque<RRWebEvent> = this.executorEvents,
217226
): ReplaySegment {
218227
val (traceIds, segmentNames) =
219228
synchronized(replayContextLock) {
@@ -261,13 +270,9 @@ constructor(
261270
val oldTimeline = clock.timeline
262271
val oldTimelineEnd = oldTimeline.nowMillis()
263272
val newTimeline = clock.restart()
264-
val oldEvents = ArrayDeque<RRWebEvent>()
265-
var event = currentEvents.pollFirst()
266-
while (event != null) {
267-
oldEvents.add(event)
268-
event = currentEvents.pollFirst()
269-
}
270-
val jump = TimelineJump(oldTimeline, oldTimelineEnd, newTimeline, oldEvents)
273+
val oldEvents = currentEvents
274+
currentEvents = ConcurrentLinkedDeque()
275+
val jump = TimelineJump(oldTimeline, oldTimelineEnd, newTimeline, oldEvents, currentEvents)
271276
StampedFrame(jump.newTimelineStart, jump)
272277
}
273278
}
@@ -312,18 +317,26 @@ constructor(
312317
)
313318

314319
/**
315-
* Where the old timeline ended and the new one starts, and the touch events stamped on the old
316-
* one, which belong to the segment that closes it.
320+
* Where the old timeline ended and the new one starts, and the touch events stamped on each. The
321+
* old timeline's events that segments queued before the jump leave belong to the segment that
322+
* closes it.
317323
*/
318324
protected class TimelineJump(
319325
val oldTimeline: AnchoredClock,
320326
val oldTimelineEnd: Long,
321327
val newTimeline: AnchoredClock,
322328
val oldEvents: Deque<RRWebEvent>,
329+
val newEvents: Deque<RRWebEvent>,
323330
) {
324331
val newTimelineStart: Long = newTimeline.startTime().epochMillis
325332
}
326333

334+
/** Called from the task of the frame that followed [jump], once the old timeline is closed. */
335+
protected fun moveExecutorOnto(jump: TimelineJump) {
336+
executorEvents = jump.newEvents
337+
executorTimeline = jump.newTimeline
338+
}
339+
327340
/**
328341
* The times a segment request reads when it is made, so a busy executor doesn't stretch the
329342
* segment. If a frame moved the recording onto a new timeline in between, what the request read

‎sentry-android-replay/src/main/java/io/sentry/android/replay/capture/BufferCaptureStrategy.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ constructor(
161161
// frames left from the old timeline would be encoded into the new one's segments
162162
cache?.rotate(until = jump.oldTimelineEnd)
163163
// last, so a segment request can't see the new timeline before the cache is on it
164-
executorTimeline = jump.newTimeline
164+
moveExecutorOnto(jump)
165165
}
166166

167167
/** Buffers the frames up to where the old timeline ended as a segment of their own. */

‎sentry-android-replay/src/main/java/io/sentry/android/replay/capture/SessionCaptureStrategy.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ constructor(
207207
}
208208
// frames left from the old timeline would be encoded into the new one's segments
209209
cache?.rotate(until = jump.oldTimelineEnd)
210-
executorTimeline = jump.newTimeline
210+
moveExecutorOnto(jump)
211211
}
212212

213213
override fun onConfigurationChanged(recorderConfig: ScreenshotRecorderConfig) {

‎sentry-android-replay/src/test/java/io/sentry/android/replay/capture/SessionCaptureStrategyTest.kt‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,29 @@ class SessionCaptureStrategyTest {
11001100
assertThat(touchesAfterJump.map { it.timestamp }).containsExactly(newTimelineStart + 100)
11011101
}
11021102

1103+
@Test
1104+
fun `touch events stay in a segment queued before a wall-clock jump`() {
1105+
val strategy = fixture.getSut(replayExecutor = fixture.queuedExecutor)
1106+
strategy.start()
1107+
strategy.onConfigurationChanged(fixture.recorderConfig)
1108+
fixture.runQueuedTasks()
1109+
val segmentStart = fixture.now
1110+
fixture.elapse(1_000)
1111+
strategy.onTouchEvent(fixture.mockedMotionEvent(MotionEvent.ACTION_DOWN))
1112+
fixture.elapse(4_000)
1113+
// its task cuts the segment the touch is in, but the executor is busy
1114+
strategy.recordFrame()
1115+
fixture.elapse(1_000)
1116+
fixture.jumpWallClock(-120_000)
1117+
strategy.recordFrame()
1118+
1119+
fixture.runQueuedTasks()
1120+
1121+
val payloads = capturedPayloads(count = 2)
1122+
assertThat(payloads[0]!!.filterIsInstance<RRWebInteractionEvent>().map { it.timestamp })
1123+
.containsExactly(segmentStart + 1_000)
1124+
}
1125+
11031126
@Test
11041127
fun `executor delay across a wall-clock jump keeps both timelines apart`() {
11051128
val strategy = fixture.getSut(replayExecutor = fixture.queuedExecutor)

0 commit comments

Comments
 (0)