Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.sentry.SentryOptions;
import io.sentry.android.core.internal.util.AndroidThreadChecker;
import io.sentry.util.AutoClosableReentrantLock;
import io.sentry.util.ExceptionUtils;
import java.io.Closeable;
import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -112,7 +113,8 @@ private void ensureLifecycleObserver(final @NotNull ILogger logger) {
SentryLevel.WARNING,
"androidx.lifecycle is not available, some features might not be properly working,"
+ "e.g. Session Tracking, Network and System Events breadcrumbs, etc.");
} catch (Throwable e) {
} catch (LinkageError e) {
// androidx.lifecycle has an incompatible version
logger.log(SentryLevel.ERROR, "AppState could not register lifecycle observer", e);
}
}
Expand All @@ -124,7 +126,7 @@ private void addObserverInternal(final @NotNull ILogger logger) {
if (observerRef != null) {
ProcessLifecycleOwner.get().getLifecycle().addObserver(observerRef);
}
} catch (Throwable e) {
} catch (LinkageError e) {
// This is to handle a potential 'AbstractMethodError' gracefully. The error is triggered in
// connection with conflicting dependencies of the androidx.lifecycle.
// //See the issue here: https://github.com/getsentry/sentry-java/pull/2228
Expand All @@ -133,6 +135,13 @@ private void addObserverInternal(final @NotNull ILogger logger) {
SentryLevel.ERROR,
"AppState failed to get Lifecycle and could not install lifecycle observer.",
e);
} catch (Throwable t) {
// addObserver dispatches ON_START synchronously to the app state listeners that are already
// registered, such as session tracking and the scope observers of hybrid SDKs, so we can't
// know what it throws. Posted from a background init, nothing else would catch it on the
// main looper. The listeners miss this ON_START.
ExceptionUtils.rethrowIfFatal(t);
logger.log(SentryLevel.ERROR, "AppState failed to notify the app state listeners.", t);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public final void register(final @NotNull IScopes scopes, final @NotNull SentryO
this.options
.getLogger()
.log(SentryLevel.ERROR, "Failed to invoke the SentryNdk.init method.", e);
} catch (Throwable e) {
} catch (ReflectiveOperationException e) {
// InvocationTargetException wraps whatever SentryNdk.init threw, such as an
// UnsatisfiedLinkError when the native library failed to load
disableNdkIntegration(this.options);
this.options.getLogger().log(SentryLevel.ERROR, "Failed to initialize SentryNdk.", e);
}
Expand Down Expand Up @@ -120,7 +122,7 @@ public void close() throws IOException {
options
.getLogger()
.log(SentryLevel.ERROR, "Failed to invoke the SentryNdk.close method.", e);
} catch (Throwable e) {
} catch (ReflectiveOperationException e) {
options.getLogger().log(SentryLevel.ERROR, "Failed to close SentryNdk.", e);
} finally {
disableNdkIntegration(this.options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.sentry.android.fragment.FragmentLifecycleIntegration;
import io.sentry.android.timber.SentryTimberIntegration;
import io.sentry.util.AutoClosableReentrantLock;
import io.sentry.util.ExceptionUtils;
import io.sentry.util.LoadClass;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
Expand Down Expand Up @@ -79,6 +80,11 @@ public static void init(@NotNull final Context context, @NotNull ILogger logger)
/**
* Sentry initialization with a configuration handler that may override the default options
*
* <p>If the configuration handler throws, init continues with the options it set before it
* failed. Fatal errors such as {@link OutOfMemoryError} are rethrown instead. The failure is only
* logged at ERROR level through the SDK logger, which prints only when debug is enabled, e.g.
* through the {@code io.sentry.debug} manifest entry.
*
* @param context Application. context
* @param configuration Sentry.OptionsConfiguration configuration handler
*/
Expand All @@ -91,6 +97,11 @@ public static void init(
/**
* Sentry initialization with a configuration handler and custom logger
*
* <p>If the configuration handler throws, init continues with the options it set before it
* failed. Fatal errors such as {@link OutOfMemoryError} are rethrown instead. The failure is only
* logged at ERROR level through the SDK logger, which prints only when debug is enabled, e.g.
* through the {@code io.sentry.debug} manifest entry.
*
* @param context Application. context
* @param logger your custom logger that implements ILogger
* @param configuration Sentry.OptionsConfiguration configuration handler
Expand Down Expand Up @@ -150,8 +161,20 @@ public static void init(

try {
configuration.configure(options);
} catch (LinkageError e) {
// The callback can touch options of optional modules that are missing or have an
// incompatible version, e.g. session replay's masking options. Init continues with
// the options set before it failed.
options
.getLogger()
.log(
SentryLevel.ERROR,
"Error in the 'OptionsConfiguration.configure' callback.",
e);
} catch (Throwable t) {
// let it slip, but log it
// The callback is the app's or a hybrid SDK's own code, so we can't know what it
// throws. Init continues with the options set before it failed.
ExceptionUtils.rethrowIfFatal(t);
options
.getLogger()
.log(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,29 @@ public static ViewHierarchy snapshotViewHierarchy(
try {
viewHierarchy.set(snapshotViewHierarchy(decorView, exporters));
latch.countDown();
} catch (LinkageError e) {
// The Compose exporter calls Compose internals, which may not match the app's
// Compose version
logger.log(SentryLevel.ERROR, "Failed to process view hierarchy.", e);
} catch (Throwable t) {
// The snapshot calls into the app's own views, so we can't know what it throws.
// The event is sent without a view hierarchy.
ExceptionUtils.rethrowIfFatal(t);
logger.log(SentryLevel.ERROR, "Failed to process view hierarchy.", t);
}
});
if (latch.await(CAPTURE_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
return viewHierarchy.get();
}
}
} catch (LinkageError e) {
// The Compose exporter calls Compose internals, which may not match the app's Compose
// version
logger.log(SentryLevel.ERROR, "Failed to process view hierarchy.", e);
} catch (Throwable t) {
// The snapshot calls into the app's own views, so we can't know what it throws. The event
// is sent without a view hierarchy.
ExceptionUtils.rethrowIfFatal(t);
logger.log(SentryLevel.ERROR, "Failed to process view hierarchy.", t);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.SpanStatus;
import io.sentry.util.ExceptionUtils;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -56,7 +57,16 @@ public boolean dispatchTouchEvent(final @Nullable MotionEvent motionEvent) {
final MotionEvent copy = motionEventObtainer.obtain(motionEvent);
try {
handleTouchEvent(copy);
} catch (LinkageError e) {
// The Compose gesture locator calls Compose internals, which may not match the app's
// Compose version
if (options != null) {
options.getLogger().log(SentryLevel.ERROR, "Error dispatching touch event", e);
}
} catch (Throwable e) {
// Locating the touched view calls into the app's own views, so we can't know what it
// throws. The gesture is not recorded, and the touch still reaches the app.
ExceptionUtils.rethrowIfFatal(e);
if (options != null) {
options.getLogger().log(SentryLevel.ERROR, "Error dispatching touch event", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.sentry.ndk.NativeModuleListLoader;
import io.sentry.protocol.DebugImage;
import io.sentry.util.AutoClosableReentrantLock;
import io.sentry.util.ExceptionUtils;
import io.sentry.util.Objects;
import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -71,7 +72,13 @@ public DebugImagesLoader(
.getLogger()
.log(SentryLevel.DEBUG, "Debug images loaded: %d", debugImages.size());
}
} catch (LinkageError e) {
// e.g. UnsatisfiedLinkError when the native library failed to load
options.getLogger().log(SentryLevel.ERROR, e, "Failed to load debug images.");
} catch (Throwable e) {
// A JNI call into sentry-native, so we can't know what it throws. The event is sent
// without debug images.
ExceptionUtils.rethrowIfFatal(e);
options.getLogger().log(SentryLevel.ERROR, e, "Failed to load debug images.");
}
}
Expand Down Expand Up @@ -172,7 +179,13 @@ public void clearDebugImages() {
moduleListLoader.clearModuleList();

options.getLogger().log(SentryLevel.INFO, "Debug images cleared.");
} catch (LinkageError e) {
// e.g. UnsatisfiedLinkError when the native library failed to load
options.getLogger().log(SentryLevel.ERROR, e, "Failed to clear debug images.");
} catch (Throwable e) {
// A JNI call into sentry-native, so we can't know what it throws. The cached list is
// cleared either way.
ExceptionUtils.rethrowIfFatal(e);
options.getLogger().log(SentryLevel.ERROR, e, "Failed to clear debug images.");
}
debugImages = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ private SentryNdk() {}
try {
//noinspection UnstableApiUsage
io.sentry.ndk.SentryNdk.loadNativeLibraries();
} catch (Throwable t) {
// ignored
// if loadLibrary() fails, the later init() will throw an exception anyway
} catch (LinkageError ignored) {
// e.g. UnsatisfiedLinkError. The later init() throws too, which disables the NDK
// integration.
} finally {
loadLibraryLatch.countDown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import io.sentry.protocol.SentryId
import io.sentry.transport.ICurrentDateProvider
import io.sentry.transport.RateLimiter
import io.sentry.transport.RateLimiter.IRateLimitObserver
import io.sentry.util.ExceptionUtils
import io.sentry.util.FileUtils
import io.sentry.util.HintUtils
import io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion
Expand Down Expand Up @@ -486,7 +487,14 @@ public class ReplayIntegration(
val hint = Hint()
hint.set(TypeCheckHint.REPLAY_FRAME_BITMAP, copy)
observer.onMaskedFrameCaptured(hint, frameTimeStamp, screen)
} catch (e: LinkageError) {
// the observer needs a class that is missing or has an incompatible version
options.logger.log(ERROR, "Error in ReplayFrameObserver", e)
copy.recycle()
} catch (e: Throwable) {
// The observer is the app's own code, so we can't know what it throws. The copy is
// recycled and the frame is still recorded.
ExceptionUtils.rethrowIfFatal(e)
options.logger.log(ERROR, "Error in ReplayFrameObserver", e)
copy.recycle()
}
Expand All @@ -509,7 +517,14 @@ public class ReplayIntegration(
val hint = Hint()
hint.set(TypeCheckHint.REPLAY_FRAME_BITMAP, bitmap)
observer.onMaskedFrameCaptured(hint, frameTimestamp, screen)
} catch (e: LinkageError) {
// the observer needs a class that is missing or has an incompatible version
options.logger.log(ERROR, "Error in ReplayFrameObserver", e)
bitmap.recycle()
} catch (e: Throwable) {
// The observer is the app's own code, so we can't know what it throws. The bitmap is
// recycled and the frame is still recorded.
ExceptionUtils.rethrowIfFatal(e)
options.logger.log(ERROR, "Error in ReplayFrameObserver", e)
bitmap.recycle()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.util.Log
import android.view.View
import android.view.Window
import io.sentry.util.AutoClosableReentrantLock
import io.sentry.util.ExceptionUtils
import java.io.Closeable
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.atomic.AtomicBoolean
Expand Down Expand Up @@ -53,7 +54,7 @@ internal object WindowSpy {
lazy(NONE) {
try {
Class.forName("com.android.internal.policy.DecorView")
} catch (ignored: Throwable) {
} catch (ignored: ClassNotFoundException) {
Log.d("WindowSpy", "Unexpected exception loading DecorView on API $SDK_INT", ignored)
null
}
Expand Down Expand Up @@ -171,7 +172,7 @@ internal object WindowManagerSpy {
val className = "android.view.WindowManagerGlobal"
try {
Class.forName(className)
} catch (ignored: Throwable) {
} catch (ignored: ClassNotFoundException) {
Log.w("WindowManagerSpy", ignored)
null
}
Expand All @@ -195,11 +196,30 @@ internal object WindowManagerSpy {
mViewsField?.let { mViewsField ->
@Suppress("UNCHECKED_CAST")
val mViews = mViewsField[windowManagerInstance] as ArrayList<View>
mViewsField[windowManagerInstance] = swap(mViews)
val swapped = swapSafely(swap, mViews) ?: return
mViewsField[windowManagerInstance] = swapped
}
}
} catch (ignored: Throwable) {
} catch (ignored: ReflectiveOperationException) {
Log.w("WindowManagerSpy", ignored)
} catch (ignored: ClassCastException) {
// mViews is not an ArrayList on this device
Log.w("WindowManagerSpy", ignored)
}
}

private fun swapSafely(
swap: (ArrayList<View>) -> ArrayList<View>,
mViews: ArrayList<View>,
): ArrayList<View>? =
try {
swap(mViews)
} catch (@Suppress("TooGenericExceptionCaught") e: Throwable) {
// swap notifies the root view listeners about the existing views, and those run replay's
// recorders, so we can't know what they throw. mViews is left as it was, so replay doesn't
// learn about root views being added or removed.
ExceptionUtils.rethrowIfFatal(e)
Log.w("WindowManagerSpy", e)
null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.findRootCoordinates
import androidx.compose.ui.node.LayoutNode
import androidx.compose.ui.text.TextLayoutResult
import io.sentry.util.ExceptionUtils
import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.roundToInt
Expand Down Expand Up @@ -93,7 +94,7 @@ internal fun LayoutNode.findPainter(): Painter? {
return try {
modifier::class.java.getDeclaredField("painter").apply { isAccessible = true }.get(modifier)
as? Painter
} catch (e: Throwable) {
} catch (_: ReflectiveOperationException) {
null
}
}
Expand Down Expand Up @@ -136,7 +137,12 @@ internal fun LayoutNode.findTextColor(): Color? {
(modifier::class.java.getDeclaredField("color").apply { isAccessible = true }.get(modifier)
as? ColorProducer)
?.invoke()
} catch (_: LinkageError) {
// older Compose versions don't have ColorProducer
null
} catch (e: Throwable) {
// ColorProducer.invoke() runs the app's color lambda, so we can't know what it throws
ExceptionUtils.rethrowIfFatal(e)
null
}
}
Expand Down
Loading
Loading