Skip to content

Commit 120662a

Browse files
runningcodeclaude
andcommitted
fix: Rethrow fatal errors at the SDK's boundary catches
The broad catches at boundaries, where we can't know what the other side throws, also swallowed OutOfMemoryError, StackOverflowError and LinkageError. They now call ExceptionUtils.rethrowIfFatal first, with a comment saying why the catch is broad and what is lost when it fails. The Javadoc of the user-facing callbacks says what happens when they throw. Sites where a LinkageError is the expected failure are left alone, so a missing or mismatched optional dependency keeps the old handling. Refs #5513 Refs JAVA-552 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e6e5918 commit 120662a

99 files changed

Lines changed: 1096 additions & 22 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎sentry-android-core/src/main/java/io/sentry/android/core/ANRWatchDog.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import io.sentry.SentryLevel;
3535
import io.sentry.time.Deadline;
3636
import io.sentry.time.MonotonicTicker;
37+
import io.sentry.util.ExceptionUtils;
3738
import java.util.List;
3839
import java.util.concurrent.TimeUnit;
3940
import java.util.concurrent.atomic.AtomicBoolean;
@@ -174,9 +175,16 @@ private boolean isProcessNotResponding() {
174175
if (am != null) {
175176
List<ActivityManager.ProcessErrorStateInfo> processesInErrorState = null;
176177
try {
177-
// It can throw RuntimeException or OutOfMemoryError
178178
processesInErrorState = am.getProcessesInErrorState();
179+
} catch (OutOfMemoryError e) {
180+
// Unparceling the reply, which lists every process in an error state, can fail to
181+
// allocate even when the app itself has memory left, so it's not treated as fatal here
182+
// (#2114). The process is treated as responding normally, so no ANR is reported.
183+
logger.log(SentryLevel.ERROR, "Error getting ActivityManager#getProcessesInErrorState.", e);
179184
} catch (Throwable e) {
185+
// A Binder call into the activity manager, which can rethrow any runtime exception from
186+
// the other side. The process is treated as responding normally, so no ANR is reported.
187+
ExceptionUtils.rethrowIfFatal(e);
180188
logger.log(SentryLevel.ERROR, "Error getting ActivityManager#getProcessesInErrorState.", e);
181189
}
182190
// if list is null, there's no process in ANR state.

‎sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.sentry.protocol.SentryThread;
3131
import io.sentry.transport.CurrentDateProvider;
3232
import io.sentry.transport.ICurrentDateProvider;
33+
import io.sentry.util.ExceptionUtils;
3334
import io.sentry.util.HintUtils;
3435
import io.sentry.util.Objects;
3536
import java.io.BufferedReader;
@@ -204,8 +205,12 @@ public void markReported(final long timestamp) {
204205
return new ParseResult(ParseResult.Type.NO_DUMP);
205206
}
206207
dump = NativeEventUtils.readBytes(trace);
207-
} catch (Throwable e) {
208-
options.getLogger().log(SentryLevel.WARNING, "Failed to read ANR thread dump", e);
208+
} catch (Throwable t) {
209+
// getTraceInputStream() is a Binder call into system_server, which can rethrow any runtime
210+
// exception from the other side. As with a missing dump, this ANR is not reported, and the
211+
// remaining exit infos are still processed.
212+
ExceptionUtils.rethrowIfFatal(t);
213+
options.getLogger().log(SentryLevel.WARNING, "Failed to read ANR thread dump", t);
209214
return new ParseResult(ParseResult.Type.NO_DUMP);
210215
}
211216

@@ -229,6 +234,10 @@ public void markReported(final long timestamp) {
229234
}
230235
return new ParseResult(ParseResult.Type.DUMP, dump, threads, debugImages, artContext);
231236
} catch (Throwable e) {
237+
// The dump is written by ART in a format that changes between Android versions and
238+
// vendors, so we can't know how the parser fails on unexpected input. The ANR is still
239+
// reported, with a message asking to attach the raw dump.
240+
ExceptionUtils.rethrowIfFatal(e);
232241
options.getLogger().log(SentryLevel.WARNING, "Failed to parse ANR thread dump", e);
233242
return new ParseResult(ParseResult.Type.ERROR, dump);
234243
}

‎sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.sentry.android.core.internal.util.Debouncer;
1616
import io.sentry.android.core.internal.util.DeviceOrientations;
1717
import io.sentry.protocol.Device;
18+
import io.sentry.util.ExceptionUtils;
1819
import io.sentry.util.Objects;
1920
import java.io.Closeable;
2021
import java.io.IOException;
@@ -66,6 +67,10 @@ public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions
6667
.log(SentryLevel.DEBUG, "AppComponentsBreadcrumbsIntegration installed.");
6768
addIntegrationToSdkVersion("AppComponentsBreadcrumbs");
6869
} catch (Throwable e) {
70+
// The context may be the app's own subclass, or a ContextImpl that doesn't support
71+
// component callbacks on older versions, so we can't know what this throws. App
72+
// component breadcrumbs are disabled.
73+
ExceptionUtils.rethrowIfFatal(e);
6974
this.options.setEnableAppComponentBreadcrumbs(false);
7075
options.getLogger().log(SentryLevel.INFO, e, "ComponentCallbacks2 is not available.");
7176
}
@@ -77,12 +82,15 @@ public void close() throws IOException {
7782
try {
7883
// if its a ContextImpl, unregisterComponentCallbacks can't be used
7984
context.unregisterComponentCallbacks(this);
80-
} catch (Throwable ignored) {
81-
// fine, might throw on older versions
85+
} catch (Throwable e) {
86+
// The context may be the app's own subclass, or a ContextImpl that doesn't support
87+
// component callbacks on older versions, so we can't know what this throws. The callbacks
88+
// may stay registered until the process ends.
89+
ExceptionUtils.rethrowIfFatal(e);
8290
if (options != null) {
8391
options
8492
.getLogger()
85-
.log(SentryLevel.DEBUG, ignored, "It was not possible to unregisterComponentCallbacks");
93+
.log(SentryLevel.DEBUG, e, "It was not possible to unregisterComponentCallbacks");
8694
}
8795
}
8896

‎sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import io.sentry.protocol.SentryTransaction;
7070
import io.sentry.protocol.User;
7171
import io.sentry.protocol.profiling.SentryProfile;
72+
import io.sentry.util.ExceptionUtils;
7273
import io.sentry.util.HintUtils;
7374
import io.sentry.util.SentryRandom;
7475
import java.io.File;
@@ -447,6 +448,9 @@ private void setApp(final @NotNull SentryBaseEvent event) {
447448
}
448449
}
449450
} catch (Throwable e) {
451+
// The first DeviceInfoUtil.getInstance call queries the package manager over Binder, which
452+
// can rethrow any runtime exception from the other side. The split APK info is left out.
453+
ExceptionUtils.rethrowIfFatal(e);
450454
options.getLogger().log(SentryLevel.ERROR, "Error getting split apks info.", e);
451455
}
452456

@@ -716,6 +720,9 @@ private void setSideLoadedInfo(final @NotNull SentryBaseEvent event) {
716720
}
717721
}
718722
} catch (Throwable e) {
723+
// The first DeviceInfoUtil.getInstance call queries the package manager over Binder, which
724+
// can rethrow any runtime exception from the other side. The side-loaded tags are left out.
725+
ExceptionUtils.rethrowIfFatal(e);
719726
options.getLogger().log(SentryLevel.ERROR, "Error getting side loaded info.", e);
720727
}
721728
}
@@ -975,6 +982,9 @@ private void applyAnrProfile(
975982
options.getLogger().log(SentryLevel.DEBUG, "No ANR profile file found");
976983
}
977984
} catch (Throwable t) {
985+
// The profile file was written by the previous process, possibly while it was being
986+
// killed, so we can't know how reading it fails. The ANR is sent without a profile.
987+
ExceptionUtils.rethrowIfFatal(t);
978988
options.getLogger().log(SentryLevel.INFO, "Could not retrieve ANR profile", t);
979989
} finally {
980990
if (!AnrProfileRotationHelper.deleteLastFile(cacheDir)) {

‎sentry-android-core/src/main/java/io/sentry/android/core/ContextUtils.java‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.sentry.SentryOptions;
2323
import io.sentry.android.core.util.AndroidLazyEvaluator;
2424
import io.sentry.protocol.App;
25+
import io.sentry.util.ExceptionUtils;
2526
import java.io.BufferedReader;
2627
import java.io.File;
2728
import java.io.FileReader;
@@ -104,6 +105,9 @@ private ContextUtils() {}
104105
.getPackageManager()
105106
.getPackageInfo(context.getPackageName(), PackageManager.PackageInfoFlags.of(0));
106107
} catch (Throwable e) {
108+
// A Binder call into the package manager, which can rethrow any runtime exception from
109+
// the other side. The package info is left out.
110+
ExceptionUtils.rethrowIfFatal(e);
107111
return null;
108112
}
109113
});
@@ -113,6 +117,9 @@ private ContextUtils() {}
113117
try {
114118
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
115119
} catch (Throwable e) {
120+
// A Binder call into the package manager, which can rethrow any runtime exception from
121+
// the other side. The package info is left out.
122+
ExceptionUtils.rethrowIfFatal(e);
116123
return null;
117124
}
118125
});
@@ -131,6 +138,9 @@ private ContextUtils() {}
131138
return context.getString(stringId);
132139
}
133140
} catch (Throwable e) {
141+
// getApplicationLabel is a Binder call into the package manager, and the context may be
142+
// the app's own subclass, so we can't know what this throws. The app name is left out.
143+
ExceptionUtils.rethrowIfFatal(e);
134144
return null;
135145
}
136146
});
@@ -150,6 +160,9 @@ private ContextUtils() {}
150160
context.getPackageName(),
151161
PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA));
152162
} catch (Throwable e) {
163+
// A Binder call into the package manager, which can rethrow any runtime exception from
164+
// the other side. The application info is left out.
165+
ExceptionUtils.rethrowIfFatal(e);
153166
return null;
154167
}
155168
});
@@ -161,6 +174,9 @@ private ContextUtils() {}
161174
.getPackageManager()
162175
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
163176
} catch (Throwable e) {
177+
// A Binder call into the package manager, which can rethrow any runtime exception from
178+
// the other side. The application info is left out.
179+
ExceptionUtils.rethrowIfFatal(e);
164180
return null;
165181
}
166182
});
@@ -202,6 +218,9 @@ static PackageInfo getPackageInfo(
202218
return context.getPackageManager().getPackageInfo(context.getPackageName(), flags);
203219
}
204220
} catch (Throwable e) {
221+
// A Binder call into the package manager, which can rethrow any runtime exception from the
222+
// other side. The package info is left out.
223+
ExceptionUtils.rethrowIfFatal(e);
205224
logger.log(SentryLevel.ERROR, "Error getting package info.", e);
206225
return null;
207226
}
@@ -269,8 +288,10 @@ public static boolean isForegroundImportance() {
269288
new ActivityManager.RunningAppProcessInfo();
270289
ActivityManager.getMyMemoryState(appProcessInfo);
271290
return appProcessInfo.importance == IMPORTANCE_FOREGROUND;
272-
} catch (Throwable ignored) {
273-
// should never happen
291+
} catch (Throwable e) {
292+
// A Binder call into the activity manager, which can rethrow any runtime exception from the
293+
// other side. The process is treated as not in the foreground.
294+
ExceptionUtils.rethrowIfFatal(e);
274295
}
275296
return false;
276297
}
@@ -299,7 +320,9 @@ public static boolean appIsLibraryForComposePreview(final @NotNull Context conte
299320
}
300321
}
301322
} catch (Throwable t) {
302-
// ignored
323+
// getAppTasks is a Binder call into the activity manager, which can rethrow any runtime
324+
// exception from the other side. The app is treated as not running a Compose Preview.
325+
ExceptionUtils.rethrowIfFatal(t);
303326
}
304327
}
305328
return false;
@@ -398,6 +421,9 @@ public static boolean appIsLibraryForComposePreview(final @NotNull Context conte
398421
try {
399422
return context.getResources().getDisplayMetrics();
400423
} catch (Throwable e) {
424+
// The context may be the app's own subclass that overrides getResources(), so we can't know
425+
// what it throws. The display metrics are left out.
426+
ExceptionUtils.rethrowIfFatal(e);
401427
logger.log(SentryLevel.ERROR, "Error getting DisplayMetrics.", e);
402428
return null;
403429
}
@@ -440,6 +466,9 @@ public static boolean appIsLibraryForComposePreview(final @NotNull Context conte
440466
logger.log(SentryLevel.INFO, "Error getting MemoryInfo.");
441467
return null;
442468
} catch (Throwable e) {
469+
// A Binder call into the activity manager, which can rethrow any runtime exception from the
470+
// other side. The memory info is left out.
471+
ExceptionUtils.rethrowIfFatal(e);
443472
logger.log(SentryLevel.ERROR, "Error getting MemoryInfo.", e);
444473
return null;
445474
}

‎sentry-android-core/src/main/java/io/sentry/android/core/DefaultAndroidEventProcessor.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.sentry.protocol.SentryThread;
1818
import io.sentry.protocol.SentryTransaction;
1919
import io.sentry.protocol.User;
20+
import io.sentry.util.ExceptionUtils;
2021
import io.sentry.util.HintUtils;
2122
import io.sentry.util.Objects;
2223
import java.util.Collections;
@@ -193,6 +194,9 @@ private void setDevice(
193194
.getContexts()
194195
.setDevice(deviceInfoUtil.get().collectDeviceInformation(errorEvent, applyScopeData));
195196
} catch (Throwable e) {
197+
// Collecting the device info makes Binder calls into several system services, which can
198+
// rethrow any runtime exception from the other side. The device context is left out.
199+
ExceptionUtils.rethrowIfFatal(e);
196200
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve device info", e);
197201
}
198202
} else {

‎sentry-android-core/src/main/java/io/sentry/android/core/DeviceInfoUtil.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.sentry.android.core.internal.util.RootChecker;
2626
import io.sentry.protocol.Device;
2727
import io.sentry.protocol.OperatingSystem;
28+
import io.sentry.util.ExceptionUtils;
2829
import java.io.File;
2930
import java.util.Calendar;
3031
import java.util.Collections;
@@ -303,6 +304,9 @@ public static Float getBatteryLevel(
303304

304305
return ((float) level / (float) scale) * percentMultiplier;
305306
} catch (Throwable e) {
307+
// The extras were parceled by system_server and are unparceled on first access, which can
308+
// throw any runtime exception on malformed data. The battery level is left out.
309+
ExceptionUtils.rethrowIfFatal(e);
306310
options.getLogger().log(SentryLevel.ERROR, "Error getting device battery level.", e);
307311
return null;
308312
}
@@ -321,6 +325,9 @@ public static Boolean isCharging(
321325
return plugged == BatteryManager.BATTERY_PLUGGED_AC
322326
|| plugged == BatteryManager.BATTERY_PLUGGED_USB;
323327
} catch (Throwable e) {
328+
// The extras were parceled by system_server and are unparceled on first access, which can
329+
// throw any runtime exception on malformed data. The charging state is left out.
330+
ExceptionUtils.rethrowIfFatal(e);
324331
options.getLogger().log(SentryLevel.ERROR, "Error getting device charging state.", e);
325332
return null;
326333
}
@@ -334,6 +341,9 @@ private Float getBatteryTemperature(final @NotNull Intent batteryIntent) {
334341
return ((float) temperature) / 10; // celsius
335342
}
336343
} catch (Throwable e) {
344+
// The extras were parceled by system_server and are unparceled on first access, which can
345+
// throw any runtime exception on malformed data. The temperature is left out.
346+
ExceptionUtils.rethrowIfFatal(e);
337347
options.getLogger().log(SentryLevel.ERROR, "Error getting battery temperature.", e);
338348
}
339349
return null;
@@ -359,6 +369,9 @@ private Device.DeviceOrientation getOrientation() {
359369
return null;
360370
}
361371
} catch (Throwable e) {
372+
// The context may be the app's own subclass that overrides getResources(), so we can't know
373+
// what it throws. The orientation is left out.
374+
ExceptionUtils.rethrowIfFatal(e);
362375
options.getLogger().log(SentryLevel.ERROR, "Error getting device orientation.", e);
363376
}
364377
return deviceOrientation;
@@ -394,6 +407,9 @@ private StatFs getExternalStorageStat(final @Nullable File internalStorage) {
394407
return new StatFs(path.getPath());
395408
}
396409
} catch (Throwable e) {
410+
// getExternalFilesDirs asks the storage service over Binder to create the directories, so
411+
// we can't know what it throws. The external storage sizes are left out.
412+
ExceptionUtils.rethrowIfFatal(e);
397413
options.getLogger().log(SentryLevel.INFO, "Not possible to read external files directory");
398414
}
399415
return null;

‎sentry-android-core/src/main/java/io/sentry/android/core/EnvelopeFileObserverIntegration.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.sentry.SentryLevel;
1111
import io.sentry.SentryOptions;
1212
import io.sentry.util.AutoClosableReentrantLock;
13+
import io.sentry.util.ExceptionUtils;
1314
import io.sentry.util.FileUtils;
1415
import io.sentry.util.Objects;
1516
import java.io.Closeable;
@@ -92,7 +93,10 @@ private void startOutboxSender(
9293
options.getLogger().log(SentryLevel.DEBUG, "EnvelopeFileObserverIntegration installed.");
9394
addIntegrationToSdkVersion("EnvelopeFileObserver");
9495
} catch (Throwable e) {
95-
// it could throw eg NoSuchFileException or NullPointerException
96+
// startWatching() sets up inotify through native code, which can throw e.g.
97+
// NoSuchFileException or NullPointerException, so we can't know what it throws. Envelopes
98+
// written to the outbox, e.g. by the NDK, are not picked up while the process runs.
99+
ExceptionUtils.rethrowIfFatal(e);
96100
options
97101
.getLogger()
98102
.log(SentryLevel.ERROR, "Failed to initialize EnvelopeFileObserverIntegration.", e);

‎sentry-android-core/src/main/java/io/sentry/android/core/FeedbackShakeIntegration.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.sentry.SentryFeedbackOptions;
1212
import io.sentry.SentryLevel;
1313
import io.sentry.SentryOptions;
14+
import io.sentry.util.ExceptionUtils;
1415
import io.sentry.util.Objects;
1516
import java.io.Closeable;
1617
import java.io.IOException;
@@ -298,6 +299,9 @@ private void startShakeDetection(final @NotNull Activity activity) {
298299
dialog = dialogFactory.create(active);
299300
dialog.show();
300301
} catch (Throwable e) {
302+
// show() adds a window over Binder and the dialog inflates with the app's theme, so
303+
// we can't know what this throws. No dialog is shown for this shake.
304+
ExceptionUtils.rethrowIfFatal(e);
301305
if (dialog != null) {
302306
onDialogGone(dialog);
303307
}

‎sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.sentry.protocol.Device;
3131
import io.sentry.protocol.SentryId;
3232
import io.sentry.protocol.User;
33+
import io.sentry.util.ExceptionUtils;
3334
import io.sentry.util.MapObjectWriter;
3435
import io.sentry.util.TracingUtils;
3536
import java.io.ByteArrayInputStream;
@@ -139,6 +140,10 @@ public static Map<String, Object> serializeScope(
139140
writer.name("level").value(logger, scope.getLevel());
140141
writer.name("breadcrumbs").value(logger, scope.getBreadcrumbs());
141142
} catch (Throwable e) {
143+
// An entry point for hybrid SDKs. Serializing the scope reaches into the device and package
144+
// manager over Binder and into whatever objects the app put in extras, so we can't know what
145+
// it throws. The caller gets an empty map.
146+
ExceptionUtils.rethrowIfFatal(e);
142147
options.getLogger().log(SentryLevel.ERROR, "Could not serialize scope.", e);
143148
return new HashMap<>();
144149
}
@@ -204,6 +209,9 @@ public static SentryId captureEnvelope(
204209
new SentryEnvelope(envelope.getHeader(), envelopeItems);
205210
return scopes.captureEnvelope(repackagedEnvelope);
206211
} catch (Throwable t) {
212+
// An entry point for hybrid SDKs, which hand over an envelope we didn't build, so we can't
213+
// know what processing it throws. The envelope is dropped and the caller gets null.
214+
ExceptionUtils.rethrowIfFatal(t);
207215
options.getLogger().log(SentryLevel.ERROR, "Failed to capture envelope", t);
208216
}
209217
return null;

0 commit comments

Comments
 (0)