@@ -42,7 +42,6 @@ import io.sentry.time.Timestamp
4242import io.sentry.transport.ICurrentDateProvider
4343import io.sentry.util.AutoClosableReentrantLock
4444import java.io.File
45- import java.util.ArrayDeque
4645import java.util.Date
4746import java.util.Deque
4847import 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
0 commit comments