Skip to content

Commit 184849c

Browse files
romtsnclaude
andcommitted
fix(replay): Share the replay ids of this process across SDK inits
A second Sentry.init registers a new ReplayIntegration, whose startup cleanup would delete the folders of replays the first instance recorded, including videos still waiting for the transport. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ef9b988 commit 184849c

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public class ReplayIntegration(
8686
SentryIntegrationPackageStorage.getInstance()
8787
.addPackage("maven:io.sentry:sentry-android-replay", BuildConfig.VERSION_NAME)
8888
}
89+
90+
// Replays recorded by this process. Their folders hold segment videos the transport may still
91+
// read after the replay stopped, so the startup cleanup of previous replays must keep them.
92+
// Shared across instances, because a second SDK init registers a new ReplayIntegration.
93+
val replayIdsOfThisProcess = CopyOnWriteArraySet<String>()
8994
}
9095

9196
// needed for the Java's call site
@@ -139,9 +144,6 @@ public class ReplayIntegration(
139144
private var mainLooperHandler: MainLooperHandler = MainLooperHandler()
140145
private var gestureRecorderProvider: (() -> GestureRecorder)? = null
141146
private val state = AtomicReference(ReplayState())
142-
// Replays recorded by this process. Their folders hold segment videos the transport may still
143-
// read after the replay stopped, so the startup cleanup of previous replays must keep them.
144-
private val replayIdsOfThisProcess = CopyOnWriteArraySet<String>()
145147

146148
override fun register(scopes: IScopes, options: SentryOptions) {
147149
this.options = options

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,30 @@ class ReplayIntegrationTest {
12951295
assertFalse(olderReplay.exists())
12961296
}
12971297

1298+
@Test
1299+
fun `second SDK init keeps replays recorded by the first one`() {
1300+
val firstReplayId = SentryId()
1301+
fixture.options.cacheDirPath = tmpDir.newFolder().absolutePath
1302+
val firstStrategy = mock<CaptureStrategy> { on { currentReplayId }.thenReturn(firstReplayId) }
1303+
val first = fixture.getSut(context, replayCaptureStrategyProvider = { firstStrategy })
1304+
first.register(fixture.scopes, fixture.options)
1305+
first.startBuffering()
1306+
val firstReplay =
1307+
File(fixture.options.cacheDirPath, "replay_$firstReplayId").also {
1308+
it.mkdirs()
1309+
File(it, "0.mp4").writeBytes(byteArrayOf(1, 2, 3))
1310+
}
1311+
first.stop()
1312+
first.close()
1313+
1314+
// a new Sentry.init registers a new integration, which runs the startup cleanup again
1315+
val secondStrategy = mock<CaptureStrategy> { on { currentReplayId }.thenReturn(SentryId()) }
1316+
val second = fixture.getSut(context, replayCaptureStrategyProvider = { secondStrategy })
1317+
second.register(fixture.scopes, fixture.options)
1318+
1319+
assertTrue(File(firstReplay, "0.mp4").exists())
1320+
}
1321+
12981322
@Test
12991323
fun `delayed startup task does not finalize a replay recorded by this process`() {
13001324
val replayId = SentryId()

0 commit comments

Comments
 (0)