@@ -36,7 +36,7 @@ final class AndroidTransactionProfiler implements ITransactionProfiler {
3636 private final int profilingTracesHz ;
3737 private final @ NotNull ISentryExecutorService executorService ;
3838 private final @ NotNull BuildInfoProvider buildInfoProvider ;
39- private final @ NotNull AtomicBoolean isInitialized = new AtomicBoolean ( false ) ;
39+ private boolean isInitialized = false ;
4040 private final @ NotNull AtomicBoolean isRunning = new AtomicBoolean (false );
4141 private final @ NotNull SentryFrameMetricsCollector frameMetricsCollector ;
4242 private @ Nullable ProfilingTransactionData currentProfilingTransactionData ;
@@ -89,9 +89,11 @@ public AndroidTransactionProfiler(
8989
9090 private void init () {
9191 // We initialize it only once
92- if (isInitialized . getAndSet ( true ) ) {
92+ if (isInitialized ) {
9393 return ;
9494 }
95+ isInitialized = true ;
96+
9597 if (!isProfilingEnabled ) {
9698 logger .log (SentryLevel .INFO , "Profiling is disabled in options." );
9799 return ;
@@ -125,11 +127,11 @@ public void start() {
125127 // causes crashes on api 21 -> https://github.com/getsentry/sentry-java/issues/3392
126128 if (buildInfoProvider .getSdkInfoVersion () < Build .VERSION_CODES .LOLLIPOP_MR1 ) return ;
127129
128- // Let's initialize trace folder and profiling interval
129- init ();
130-
131130 // When the first transaction is starting, we can start profiling
132131 if (!isRunning .getAndSet (true )) {
132+ // Let's initialize trace folder and profiling interval
133+ init ();
134+
133135 if (onFirstStart ()) {
134136 logger .log (SentryLevel .DEBUG , "Profiler started." );
135137 } else {
@@ -138,6 +140,10 @@ public void start() {
138140 logger .log (
139141 SentryLevel .WARNING , "A profile is already running. This profile will be ignored." );
140142 } else {
143+ try (final @ NotNull ISentryLifecycleToken ignored = lock .acquire ()) {
144+ // Ensure we unbind any transaction data, just in case of concurrent starts
145+ currentProfilingTransactionData = null ;
146+ }
141147 // Otherwise we update the flag, because it means the profiler is not running
142148 isRunning .set (false );
143149 }
0 commit comments