Skip to content

Commit ada121f

Browse files
committed
Handle isStopped and isClosed separately
1 parent 4403f3e commit ada121f

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public final class SystemEventsBreadcrumbsIntegration implements Integration, Cl
6767
private @Nullable IScopes scopes;
6868

6969
private final @NotNull String[] actions;
70+
private volatile boolean isClosed = false;
7071
private volatile boolean isStopped = false;
7172
private volatile IntentFilter filter = null;
7273
private final @NotNull AutoClosableReentrantLock receiverLock = new AutoClosableReentrantLock();
@@ -129,7 +130,7 @@ private void registerReceiver(
129130
}
130131

131132
try (final @NotNull ISentryLifecycleToken ignored = receiverLock.acquire()) {
132-
if (isStopped || receiver != null) {
133+
if (isClosed || isStopped || receiver != null) {
133134
return;
134135
}
135136
}
@@ -140,7 +141,7 @@ private void registerReceiver(
140141
.submit(
141142
() -> {
142143
try (final @NotNull ISentryLifecycleToken ignored = receiverLock.acquire()) {
143-
if (isStopped || receiver != null) {
144+
if (isClosed || isStopped || receiver != null) {
144145
return;
145146
}
146147

@@ -265,6 +266,7 @@ private void removeObserverInternal() {
265266
@Override
266267
public void close() throws IOException {
267268
try (final @NotNull ISentryLifecycleToken ignored = receiverLock.acquire()) {
269+
isClosed = true;
268270
filter = null;
269271
}
270272

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,4 +389,31 @@ class SystemEventsBreadcrumbsIntegrationTest {
389389
deferredExecutorService.runAll()
390390
assertNull(sut.receiver)
391391
}
392+
393+
@Test
394+
fun `when enters foreground right after closing, receiver is not registered`() {
395+
val deferredExecutorService = DeferredExecutorService()
396+
val latch = CountDownLatch(1)
397+
398+
val sut = fixture.getSut(executorService = deferredExecutorService, mockHandler = false)
399+
sut.register(fixture.scopes, fixture.options)
400+
deferredExecutorService.runAll()
401+
assertNotNull(sut.receiver)
402+
403+
Thread {
404+
sut.close()
405+
latch.countDown()
406+
}.start()
407+
408+
latch.await()
409+
410+
sut.lifecycleHandler!!.onStart(mock())
411+
assertNull(sut.receiver)
412+
deferredExecutorService.runAll()
413+
414+
shadowOf(Looper.getMainLooper()).idle()
415+
416+
assertNull(sut.receiver)
417+
assertNull(sut.lifecycleHandler)
418+
}
392419
}

0 commit comments

Comments
 (0)