Skip to content
Closed
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@

- Nav3: Returning to an Activity or Fragment with its own `SentryNavEffect` within the app (e.g., closing the Activity that covered it, swiping back to its ViewPager2 page, or dismissing a dialog over it) now starts a new navigation transaction for the current destination if the previous one finished, so spans produced upon return can now be consistently captured ([#6482](https://github.com/getsentry/sentry-java/pull/6482))
- Nav3: `ui.load` transactions are bound to `IScope` in preference to Nav3 transactions when transitioning between Activities, matching Nav2 behavior (API 29+) ([#6484](https://github.com/getsentry/sentry-java/pull/6484))
- The SDK no longer swallows fatal errors such as `OutOfMemoryError` and `StackOverflowError`. They now propagate to the caller, for example out of `Sentry.captureException` or from a throwing `beforeSend` callback, instead of only being logged ([#6657](https://github.com/getsentry/sentry-java/pull/6657))
- `LinkageError`s propagate too, except where the SDK expects them because an optional dependency is missing or has an incompatible version.
- Other exceptions thrown by your callbacks are still caught and logged at `ERROR`, which prints only with `debug` enabled.

### Improvements

Expand Down
5 changes: 5 additions & 0 deletions sentry/src/main/java/io/sentry/DirectoryProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.sentry.hints.Retryable;
import io.sentry.hints.SubmissionResult;
import io.sentry.transport.RateLimiter;
import io.sentry.util.ExceptionUtils;
import io.sentry.util.HintUtils;
import java.io.File;
import java.util.Queue;
Expand Down Expand Up @@ -101,6 +102,10 @@ public void processDirectory(final @NotNull File directory) {
"Thread interrupted during processing '%s'",
directory.getAbsolutePath());
} catch (Throwable e) {
// Processing a file deserializes data written by an earlier launch, another process or a
// hybrid SDK and captures it, so we can't know what it throws. The remaining files are left
// for the next run.
ExceptionUtils.rethrowIfFatal(e);
logger.log(SentryLevel.ERROR, e, "Failed processing '%s'", directory.getAbsolutePath());
}
}
Expand Down
4 changes: 4 additions & 0 deletions sentry/src/main/java/io/sentry/EnvelopeSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.sentry.cache.EnvelopeCache;
import io.sentry.hints.Flushable;
import io.sentry.hints.Retryable;
import io.sentry.util.ExceptionUtils;
import io.sentry.util.HintUtils;
import io.sentry.util.Objects;
import java.io.BufferedInputStream;
Expand Down Expand Up @@ -77,6 +78,9 @@ protected void processFile(final @NotNull File file, final @NotNull Hint hint) {
} catch (IOException e) {
logger.log(SentryLevel.ERROR, e, "I/O on file '%s' failed.", file.getAbsolutePath());
} catch (Throwable e) {
// The cached file may be corrupt or come from an older SDK version, so we can't know what
// deserializing and capturing it throws. The file is deleted instead of retried.
ExceptionUtils.rethrowIfFatal(e);
logger.log(
SentryLevel.ERROR, e, "Failed to capture cached envelope %s", file.getAbsolutePath());
HintUtils.runIfHasTypeLogIfNot(
Expand Down
5 changes: 5 additions & 0 deletions sentry/src/main/java/io/sentry/EventProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

/**
* An Event Processor that may mutate or drop an event. Runs for SentryEvent or SentryTransaction
*
* <p>If a processor throws, the event is dropped and the exception is logged at {@link
* SentryLevel#ERROR} through {@link SentryOptions#getLogger()}, which by default prints only when
* {@link SentryOptions#isDebug()} is enabled. Fatal errors, such as {@link OutOfMemoryError}, are
* rethrown.
*/
public interface EventProcessor {

Expand Down
4 changes: 4 additions & 0 deletions sentry/src/main/java/io/sentry/JsonSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.sentry.rrweb.RRWebMetaEvent;
import io.sentry.rrweb.RRWebSpanEvent;
import io.sentry.rrweb.RRWebVideoEvent;
import io.sentry.util.ExceptionUtils;
import io.sentry.util.Objects;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
Expand Down Expand Up @@ -168,6 +169,9 @@ public JsonSerializer(@NotNull SentryOptions options) {
return (T) jsonObjectReader.nextObjectOrNull();
}
} catch (Throwable e) {
// The JSON may be corrupt and the element deserializer is supplied by the caller, so we
// can't know what this throws. The collection is treated as absent.
ExceptionUtils.rethrowIfFatal(e);
options.getLogger().log(SentryLevel.ERROR, "Error when deserializing", e);
return null;
}
Expand Down
7 changes: 7 additions & 0 deletions sentry/src/main/java/io/sentry/OutboxSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.sentry.protocol.SentryId;
import io.sentry.protocol.SentryTransaction;
import io.sentry.util.CollectionUtils;
import io.sentry.util.ExceptionUtils;
import io.sentry.util.HintUtils;
import io.sentry.util.LogUtils;
import io.sentry.util.Objects;
Expand Down Expand Up @@ -153,6 +154,9 @@ private void processEnvelope(final @NotNull SentryEnvelope envelope, final @NotN
}
}
} catch (Throwable e) {
// Outbox items are written by other processes, such as the native SDK or a hybrid SDK,
// so we can't know what deserializing and capturing them throws. The item is skipped.
ExceptionUtils.rethrowIfFatal(e);
logger.log(ERROR, "Item failed to process.", e);
}
} else if (SentryItemType.Transaction.equals(item.getHeader().getType())) {
Expand Down Expand Up @@ -190,6 +194,9 @@ private void processEnvelope(final @NotNull SentryEnvelope envelope, final @NotN
}
}
} catch (Throwable e) {
// Outbox items are written by other processes, such as the native SDK or a hybrid SDK,
// so we can't know what deserializing and capturing them throws. The item is skipped.
ExceptionUtils.rethrowIfFatal(e);
logger.log(ERROR, "Item failed to process.", e);
}
} else {
Expand Down
5 changes: 5 additions & 0 deletions sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import io.sentry.cache.EnvelopeCache;
import io.sentry.cache.IEnvelopeCache;
import io.sentry.util.ExceptionUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -127,6 +128,10 @@ public void run() {
scopes.captureEnvelope(fromSession);
}
} catch (Throwable e) {
// The session file was written by an earlier launch and may be corrupt, so we can't know
// what deserializing it throws. The previous session is not sent, and its file is still
// deleted below.
ExceptionUtils.rethrowIfFatal(e);
options.getLogger().log(SentryLevel.ERROR, "Error processing previous session.", e);
}

Expand Down
7 changes: 5 additions & 2 deletions sentry/src/main/java/io/sentry/ProfilingTraceData.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.sentry;

import io.sentry.profilemeasurements.ProfileMeasurement;
import io.sentry.util.ExceptionUtils;
import io.sentry.vendor.gson.stream.JsonToken;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -365,8 +366,10 @@ public void setSampledProfile(final @Nullable String sampledProfile) {
public void readDeviceCpuFrequencies() {
try {
this.deviceCpuFrequencies = deviceCpuFrequenciesReader.call();
} catch (Throwable ignored) {
// should never happen
} catch (Throwable t) {
// The reader is supplied by the platform integration and declares any Exception, so we
// can't know what it throws. The profile is sent without CPU frequencies.
ExceptionUtils.rethrowIfFatal(t);
}
}

Expand Down
3 changes: 3 additions & 0 deletions sentry/src/main/java/io/sentry/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,9 @@ public Queue<Breadcrumb> getBreadcrumbs() {
try (final @NotNull ISentryLifecycleToken ignored = SentryCallbackReentrancyGuard.enter()) {
return callback.execute(breadcrumb, hint);
} catch (Throwable e) {
// A user callback, so we can't know what it throws. As the spec asks, the breadcrumb is
// dropped.
ExceptionUtils.rethrowIfFatal(e);
options
.getLogger()
.log(
Expand Down
11 changes: 11 additions & 0 deletions sentry/src/main/java/io/sentry/ScopeCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,16 @@
import org.jetbrains.annotations.NotNull;

public interface ScopeCallback {
/**
* Runs with the scope to read or change.
*
* <p>If this callback throws, the rest of it is skipped and the exception is logged at {@link
* SentryLevel#ERROR} through {@link SentryOptions#getLogger()}, which by default prints only when
* {@link SentryOptions#isDebug()} is enabled. When the callback was passed to a capture method,
* the event is captured with the scope as it was before the callback ran. Fatal errors, such as
* {@link OutOfMemoryError}, are rethrown.
*
* @param scope the scope
*/
void run(@NotNull IScope scope);
}
61 changes: 61 additions & 0 deletions sentry/src/main/java/io/sentry/Scopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.sentry.metrics.MetricsApi;
import io.sentry.protocol.*;
import io.sentry.transport.RateLimiter;
import io.sentry.util.ExceptionUtils;
import io.sentry.util.HintUtils;
import io.sentry.util.Objects;
import io.sentry.util.SpanUtils;
Expand Down Expand Up @@ -164,6 +165,9 @@ public boolean isEnabled() {
sentryId = getClient().captureEvent(event, localScope, hint);
updateLastEventId(sentryId);
} catch (Throwable e) {
// A public API entry point: capturing runs integration and app code, such as event
// processors, so we can't know what it throws. The event is dropped.
ExceptionUtils.rethrowIfFatal(e);
getOptions()
.getLogger()
.log(
Expand Down Expand Up @@ -192,6 +196,9 @@ private void assignTraceContext(final @NotNull SentryEvent event) {
callback.run(localScope);
return localScope;
} catch (Throwable t) {
// A user callback, so we can't know what it throws. The event is captured with the
// unchanged scope.
ExceptionUtils.rethrowIfFatal(t);
getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Error in the 'ScopeCallback' callback.", t);
Expand Down Expand Up @@ -235,6 +242,9 @@ private void assignTraceContext(final @NotNull SentryEvent event) {

sentryId = getClient().captureMessage(message, level, localScope);
} catch (Throwable e) {
// A public API entry point: capturing runs integration and app code, such as event
// processors, so we can't know what it throws. The message is dropped.
ExceptionUtils.rethrowIfFatal(e);
getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Error while capturing message: " + message, e);
Expand Down Expand Up @@ -266,6 +276,9 @@ private void assignTraceContext(final @NotNull SentryEvent event) {

sentryId = getClient().captureFeedback(feedback, hint, localScope);
} catch (Throwable e) {
// A public API entry point: capturing runs integration and app code, such as event
// processors, so we can't know what it throws. The feedback is dropped.
ExceptionUtils.rethrowIfFatal(e);
getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Error while capturing feedback: " + feedback.getMessage(), e);
Expand Down Expand Up @@ -294,6 +307,9 @@ private void assignTraceContext(final @NotNull SentryEvent event) {
sentryId = capturedEnvelopeId;
}
} catch (Throwable e) {
// An entry point for hybrid SDKs and cached envelopes, whose items may run their own data
// factories, so we can't know what it throws. The envelope is dropped.
ExceptionUtils.rethrowIfFatal(e);
getOptions().getLogger().log(SentryLevel.ERROR, "Error while capturing envelope.", e);
}
}
Expand Down Expand Up @@ -339,6 +355,9 @@ private void assignTraceContext(final @NotNull SentryEvent event) {

sentryId = getClient().captureEvent(event, localScope, hint);
} catch (Throwable e) {
// A public API entry point: capturing runs integration and app code, such as event
// processors, so we can't know what it throws. The exception is not reported.
ExceptionUtils.rethrowIfFatal(e);
getOptions()
.getLogger()
.log(
Expand Down Expand Up @@ -417,7 +436,16 @@ public void close(final boolean isRestarting) {
if (integration instanceof Closeable) {
try {
((Closeable) integration).close();
} catch (LinkageError e) {
// The integration needs an optional dependency that is missing or has an
// incompatible version. The remaining integrations are still closed.
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "Failed to close the integration {}.", integration, e);
} catch (Throwable e) {
// Integrations can be user-provided, so we can't know what they throw. The
// remaining integrations are still closed.
ExceptionUtils.rethrowIfFatal(e);
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "Failed to close the integration {}.", integration, e);
Expand All @@ -429,6 +457,9 @@ public void close(final boolean isRestarting) {
try {
((Closeable) eventProcessor).close();
} catch (Throwable e) {
// Event processors can be user-provided, so we can't know what they throw. The
// remaining event processors are still closed.
ExceptionUtils.rethrowIfFatal(e);
getOptions()
.getLogger()
.log(
Expand Down Expand Up @@ -476,6 +507,9 @@ public void close(final boolean isRestarting) {
configureScope(ScopeType.ISOLATION, scope -> scope.getClient().close(isRestarting));
configureScope(ScopeType.GLOBAL, scope -> scope.getClient().close(isRestarting));
} catch (Throwable e) {
// A public API entry point: closing shuts down components that integrations and the app
// can replace, so we can't know what it throws. The remaining shutdown steps are skipped.
ExceptionUtils.rethrowIfFatal(e);
getOptions().getLogger().log(SentryLevel.ERROR, "Error while closing the Scopes.", e);
}
}
Expand Down Expand Up @@ -670,6 +704,8 @@ public void withScope(final @NotNull ScopeCallback callback) {
try {
callback.run(NoOpScope.getInstance());
} catch (Throwable e) {
// A user callback, so we can't know what it throws. The rest of the callback is skipped.
ExceptionUtils.rethrowIfFatal(e);
getOptions().getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e);
}

Expand All @@ -678,6 +714,8 @@ public void withScope(final @NotNull ScopeCallback callback) {
try (final @NotNull ISentryLifecycleToken ignored = forkedScopes.makeCurrent()) {
callback.run(forkedScopes.getScope());
} catch (Throwable e) {
// A user callback, so we can't know what it throws. The rest of the callback is skipped.
ExceptionUtils.rethrowIfFatal(e);
getOptions().getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e);
}
}
Expand All @@ -689,6 +727,8 @@ public void withIsolationScope(final @NotNull ScopeCallback callback) {
try {
callback.run(NoOpScope.getInstance());
} catch (Throwable e) {
// A user callback, so we can't know what it throws. The rest of the callback is skipped.
ExceptionUtils.rethrowIfFatal(e);
getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Error in the 'withIsolationScope' callback.", e);
Expand All @@ -699,6 +739,8 @@ public void withIsolationScope(final @NotNull ScopeCallback callback) {
try (final @NotNull ISentryLifecycleToken ignored = forkedScopes.makeCurrent()) {
callback.run(forkedScopes.getIsolationScope());
} catch (Throwable e) {
// A user callback, so we can't know what it throws. The rest of the callback is skipped.
ExceptionUtils.rethrowIfFatal(e);
getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Error in the 'withIsolationScope' callback.", e);
Expand All @@ -719,6 +761,8 @@ public void configureScope(
try {
callback.run(combinedScope.getSpecificScope(scopeType));
} catch (Throwable e) {
// A user callback, so we can't know what it throws. The rest of the callback is skipped.
ExceptionUtils.rethrowIfFatal(e);
getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Error in the 'configureScope' callback.", e);
Expand Down Expand Up @@ -752,6 +796,9 @@ public void flush(long timeoutMillis) {
try {
getClient().flush(timeoutMillis);
} catch (Throwable e) {
// A public API entry point: flushing runs the transport, which integrations and the app can
// replace, so we can't know what it throws. Whatever was not flushed stays queued.
ExceptionUtils.rethrowIfFatal(e);
getOptions().getLogger().log(SentryLevel.ERROR, "Error in the 'client.flush'.", e);
}
}
Expand Down Expand Up @@ -821,6 +868,10 @@ public void flush(long timeoutMillis) {
hint,
profilingTraceData);
} catch (Throwable e) {
// An entry point that finishing a transaction calls on the app's thread: capturing
// runs integration and app code, such as event processors, so we can't know what it
// throws. The transaction is dropped.
ExceptionUtils.rethrowIfFatal(e);
getOptions()
.getLogger()
.log(
Expand Down Expand Up @@ -851,6 +902,10 @@ public void flush(long timeoutMillis) {
try {
sentryId = getClient().captureProfileChunk(profilingContinuousData, getCombinedScopeView());
} catch (Throwable e) {
// Capturing reads and converts the profiler's trace file, possibly with a third-party
// converter, and runs event processors, so we can't know what it throws. The chunk is
// dropped.
ExceptionUtils.rethrowIfFatal(e);
getOptions()
.getLogger()
.log(
Expand Down Expand Up @@ -1145,6 +1200,9 @@ public void reportFullyDisplayed() {
try {
sentryId = getClient().captureCheckIn(checkIn, getCombinedScopeView(), null);
} catch (Throwable e) {
// A public API entry point: capturing runs integration and app code, such as the
// transport, so we can't know what it throws. The check-in is dropped.
ExceptionUtils.rethrowIfFatal(e);
getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Error while capturing check-in for slug", e);
Expand All @@ -1168,6 +1226,9 @@ public void reportFullyDisplayed() {
try {
sentryId = getClient().captureReplayEvent(replay, getCombinedScopeView(), hint);
} catch (Throwable e) {
// Capturing runs integration and app code, such as event processors, so we can't know
// what it throws. The replay segment is dropped.
ExceptionUtils.rethrowIfFatal(e);
getOptions().getLogger().log(SentryLevel.ERROR, "Error while capturing replay", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import io.sentry.transport.RateLimiter;
import io.sentry.util.AutoClosableReentrantLock;
import io.sentry.util.ExceptionUtils;
import io.sentry.util.Objects;
import java.io.Closeable;
import java.io.File;
Expand Down Expand Up @@ -158,6 +159,10 @@ private void sendCachedEnvelopes(

sender.send();
} catch (Throwable e) {
// The sender comes from a factory that integrations and the app can provide, and it
// deserializes and captures cached files, so we can't know what it throws. The
// cached events stay on disk for the next attempt.
ExceptionUtils.rethrowIfFatal(e);
options.getLogger().log(SentryLevel.ERROR, "Failed trying to send cached events.", e);
}
});
Expand Down
Loading
Loading