Skip to content

Commit a9847ae

Browse files
43jayclaude
andcommitted
fix(profiling): AndroidContinuousProfiler Reset shouldStop flag on startProfiler
Same fix as PerfettoContinuousProfiler — shouldStop was never reset to false after stopProfiler, so a stop/start cycle would leave shouldStop=true and silently stop the profiler after one chunk. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent bb0ff41 commit a9847ae

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/AndroidContinuousProfiler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public void startProfiler(
117117
final @NotNull ProfileLifecycle profileLifecycle,
118118
final @NotNull TracesSampler tracesSampler) {
119119
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
120+
shouldStop = false;
120121
if (shouldSample) {
121122
isSampled = tracesSampler.sampleSessionProfile(SentryRandom.current().nextDouble());
122123
shouldSample = false;

sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,24 @@ class AndroidContinuousProfilerTest {
556556
.log(eq(SentryLevel.WARNING), eq("Device is offline. Stopping profiler."))
557557
}
558558

559+
@Test
560+
fun `manual profiler can be started again after a full start-stop cycle`() {
561+
val profiler = fixture.getSut()
562+
563+
profiler.startProfiler(ProfileLifecycle.MANUAL, fixture.mockTracesSampler)
564+
assertTrue(profiler.isRunning)
565+
profiler.stopProfiler(ProfileLifecycle.MANUAL)
566+
// Triggers the scheduled executor task that collects perf metrics and finalizes the chunk
567+
fixture.executor.runAll()
568+
assertFalse(profiler.isRunning)
569+
570+
profiler.startProfiler(ProfileLifecycle.MANUAL, fixture.mockTracesSampler)
571+
assertTrue(profiler.isRunning)
572+
// Triggers the scheduled executor task that collects perf metrics and finalizes the chunk
573+
fixture.executor.runAll()
574+
assertTrue(profiler.isRunning, "shouldStop must be reset on start")
575+
}
576+
559577
fun withMockScopes(closure: () -> Unit) =
560578
Mockito.mockStatic(Sentry::class.java).use {
561579
it.`when`<Any> { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes)

0 commit comments

Comments
 (0)