Skip to content

Commit 663a626

Browse files
committed
fix(android): Don't throw from the telephony executor
The SDK executor rejects work once it is shut down, and its queue check doesn't cover that, so a display info change arriving after Sentry.close threw RejectedExecutionException on a thread owned by the telephony framework.
1 parent d035a85 commit 663a626

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎sentry-android-core/src/main/java/io/sentry/android/core/internal/util/AndroidConnectionStatusProvider.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.sentry.util.AutoClosableReentrantLock;
2525
import java.util.ArrayList;
2626
import java.util.List;
27+
import java.util.concurrent.RejectedExecutionException;
2728
import java.util.concurrent.TimeUnit;
2829
import java.util.concurrent.atomic.AtomicBoolean;
2930
import org.jetbrains.annotations.ApiStatus;
@@ -100,7 +101,21 @@ public AndroidConnectionStatusProvider(
100101
this.context,
101102
options.getLogger(),
102103
buildInfoProvider,
103-
runnable -> options.getExecutorService().submit(runnable));
104+
runnable -> {
105+
try {
106+
options.getExecutorService().submit(runnable);
107+
} catch (RejectedExecutionException e) {
108+
// The telephony framework calls this executor, so an exception would be thrown on
109+
// one of its threads. The SDK's executor rejects work once it is shut down, which
110+
// can happen before the listener is unregistered.
111+
options
112+
.getLogger()
113+
.log(
114+
SentryLevel.DEBUG,
115+
"Dropping a cellular network technology update, the executor rejected it.",
116+
e);
117+
}
118+
});
104119

105120
capabilities[0] = NetworkCapabilities.NET_CAPABILITY_INTERNET;
106121
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.M) {

‎sentry-android-core/src/test/java/io/sentry/android/core/internal/util/AndroidConnectionStatusProviderTest.kt‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ import android.telephony.TelephonyManager
2121
import androidx.test.ext.junit.runners.AndroidJUnit4
2222
import io.sentry.IConnectionStatusProvider
2323
import io.sentry.ILogger
24+
import io.sentry.ISentryExecutorService
2425
import io.sentry.SentryOptions
2526
import io.sentry.android.core.AppState
2627
import io.sentry.android.core.BuildInfoProvider
2728
import io.sentry.android.core.ContextUtils
2829
import io.sentry.android.core.SystemEventsBreadcrumbsIntegration
2930
import io.sentry.test.ImmediateExecutorService
3031
import io.sentry.time.TestMonotonicTicker
32+
import java.util.concurrent.Executor
33+
import java.util.concurrent.Future
34+
import java.util.concurrent.RejectedExecutionException
3135
import java.util.concurrent.TimeUnit.MINUTES
3236
import kotlin.test.AfterTest
3337
import kotlin.test.BeforeTest
@@ -254,6 +258,41 @@ class AndroidConnectionStatusProviderTest {
254258
assertEquals("cellular", connectionStatusProvider.connectionType)
255259
}
256260

261+
/// Lets a test reject work only after the setup that needs a working executor is done.
262+
private class RejectingExecutorService(private val delegate: ISentryExecutorService) :
263+
ISentryExecutorService by delegate {
264+
var reject = false
265+
266+
override fun submit(runnable: Runnable): Future<*> {
267+
if (reject) {
268+
throw RejectedExecutionException("closed")
269+
}
270+
return delegate.submit(runnable)
271+
}
272+
}
273+
274+
@Test
275+
@Config(sdk = [Build.VERSION_CODES.S])
276+
fun `When the executor rejects work, a technology update does not throw`() {
277+
// The telephony framework calls the executor, so throwing there would surface on one of its
278+
// threads. The SDK executor rejects work once it is shut down.
279+
val telephonyManager = mock<TelephonyManager>()
280+
whenever(contextMock.getSystemService(eq(Context.TELEPHONY_SERVICE)))
281+
.thenReturn(telephonyManager)
282+
whenever(buildInfo.sdkInfoVersion).thenReturn(Build.VERSION_CODES.S)
283+
val executor = RejectingExecutorService(ImmediateExecutorService())
284+
options.executorService = executor
285+
286+
val provider = AndroidConnectionStatusProvider(contextMock, options, buildInfo, ticker)
287+
val captor = argumentCaptor<Executor>()
288+
verify(telephonyManager).registerTelephonyCallback(captor.capture(), any())
289+
executor.reject = true
290+
291+
captor.firstValue.execute {}
292+
293+
provider.close()
294+
}
295+
257296
@Test
258297
@Config(sdk = [Build.VERSION_CODES.S])
259298
fun `When the network callback cannot be registered, the telephony listener is not started`() {

0 commit comments

Comments
 (0)