diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidContinuousProfiler.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidContinuousProfiler.java index 41362c9d93..022cd088de 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidContinuousProfiler.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidContinuousProfiler.java @@ -117,6 +117,7 @@ public void startProfiler( final @NotNull ProfileLifecycle profileLifecycle, final @NotNull TracesSampler tracesSampler) { try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { + shouldStop = false; if (shouldSample) { isSampled = tracesSampler.sampleSessionProfile(SentryRandom.current().nextDouble()); shouldSample = false; diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt index 162e56c36e..70dc651951 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt @@ -556,6 +556,24 @@ class AndroidContinuousProfilerTest { .log(eq(SentryLevel.WARNING), eq("Device is offline. Stopping profiler.")) } + @Test + fun `manual profiler can be started again after a full start-stop cycle`() { + val profiler = fixture.getSut() + + profiler.startProfiler(ProfileLifecycle.MANUAL, fixture.mockTracesSampler) + assertTrue(profiler.isRunning) + profiler.stopProfiler(ProfileLifecycle.MANUAL) + // Triggers the scheduled executor task that collects perf metrics and finalizes the chunk + fixture.executor.runAll() + assertFalse(profiler.isRunning) + + profiler.startProfiler(ProfileLifecycle.MANUAL, fixture.mockTracesSampler) + assertTrue(profiler.isRunning) + // Triggers the scheduled executor task that collects perf metrics and finalizes the chunk + fixture.executor.runAll() + assertTrue(profiler.isRunning, "shouldStop must be reset on start") + } + fun withMockScopes(closure: () -> Unit) = Mockito.mockStatic(Sentry::class.java).use { it.`when` { Sentry.getCurrentScopes() }.thenReturn(fixture.scopes)