Skip to content

Commit 706fa69

Browse files
authored
Fix SDK init crash if initialized from background thread (#4449)
* Fix GestureDetectorCompat crashes if initialized from background thread * Add tests * Update Changelog
1 parent 4e0940e commit 706fa69

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixes
6+
7+
- Fix SDK init crash if initialized from background thread while an activiy is resumed ([#4449](https://github.com/getsentry/sentry-java/pull/4449))
8+
59
### Dependencies
610

711
- Bump Gradle from v8.14 to v8.14.1 ([#4437](https://github.com/getsentry/sentry-java/pull/4437))

sentry-android-core/src/main/java/io/sentry/android/core/internal/gestures/SentryWindowCallback.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.sentry.android.core.internal.gestures;
22

33
import android.content.Context;
4+
import android.os.Handler;
5+
import android.os.Looper;
46
import android.view.MotionEvent;
57
import android.view.Window;
68
import androidx.core.view.GestureDetectorCompat;
@@ -27,7 +29,7 @@ public SentryWindowCallback(
2729
final @Nullable SentryOptions options) {
2830
this(
2931
delegate,
30-
new GestureDetectorCompat(context, gestureListener),
32+
new GestureDetectorCompat(context, gestureListener, new Handler(Looper.getMainLooper())),
3133
gestureListener,
3234
options,
3335
new MotionEventObtainer() {});

sentry/src/main/java/io/sentry/Sentry.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,16 @@ private static void init(final @NotNull SentryOptions options, final boolean glo
345345
// and Scopes was still NoOp.
346346
// Registering integrations here make sure that Scopes is already created.
347347
for (final Integration integration : options.getIntegrations()) {
348-
integration.register(ScopesAdapter.getInstance(), options);
348+
try {
349+
integration.register(ScopesAdapter.getInstance(), options);
350+
} catch (Throwable t) {
351+
options
352+
.getLogger()
353+
.log(
354+
SentryLevel.WARNING,
355+
"Failed to register the integration " + integration.getClass().getName(),
356+
t);
357+
}
349358
}
350359

351360
notifyOptionsObservers(options);

sentry/src/test/java/io/sentry/SentryTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,27 @@ class SentryTest {
125125
verify(integration2).close()
126126
}
127127

128+
@Test
129+
fun `if a single integration crashes, the SDK and other integrations are still initialized`() {
130+
val goodIntegrationInitialized = AtomicBoolean(false)
131+
val goodIntegration = Integration { scopes, options ->
132+
// no-op
133+
goodIntegrationInitialized.set(true)
134+
}
135+
136+
val badIntegration = Integration { scopes, options -> throw IllegalStateException("bad integration") }
137+
138+
Sentry.init {
139+
it.dsn = dsn
140+
it.integrations.clear()
141+
it.integrations.add(badIntegration)
142+
it.integrations.add(goodIntegration)
143+
}
144+
145+
assertTrue(Sentry.isEnabled())
146+
assertTrue(goodIntegrationInitialized.get())
147+
}
148+
128149
interface CloseableIntegration : Integration, Closeable
129150

130151
@Test

0 commit comments

Comments
 (0)