Skip to content

Commit ca08d44

Browse files
authored
fix(logcat): Add re-entrancy guard to SentryLogcatAdapter
1 parent 61ba1d5 commit ca08d44

1 file changed

Lines changed: 32 additions & 9 deletions

File tree

sentry-android-core/src/main/java/io/sentry/android/core/SentryLogcatAdapter.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@
1919
@ApiStatus.Internal
2020
public final class SentryLogcatAdapter {
2121

22+
/**
23+
* Re-entrancy guard to prevent infinite recursion. When the Sentry Android Gradle Plugin
24+
* replaces Log.* calls with SentryLogcatAdapter.* calls, it also transforms calls inside the
25+
* Sentry SDK itself (e.g. AndroidLogger). This can cause a cycle:
26+
* SentryLogcatAdapter.w() -> addBreadcrumb() -> executeBeforeBreadcrumb() (on exception) ->
27+
* DiagnosticLogger.log() -> AndroidLogger.log() -> Log.w() -> SentryLogcatAdapter.w() -> ...
28+
*/
29+
private static final ThreadLocal<Boolean> isAddingBreadcrumb =
30+
new ThreadLocal<Boolean>() {
31+
@Override
32+
protected Boolean initialValue() {
33+
return Boolean.FALSE;
34+
}
35+
};
36+
2237
private static void addAsBreadcrumb(
2338
@Nullable String tag, @NotNull SentryLevel level, @Nullable String msg) {
2439
addAsBreadcrumb(tag, level, msg, null);
@@ -34,17 +49,25 @@ private static void addAsBreadcrumb(
3449
@NotNull final SentryLevel level,
3550
@Nullable final String msg,
3651
@Nullable final Throwable tr) {
37-
Breadcrumb breadcrumb = new Breadcrumb();
38-
breadcrumb.setCategory("Logcat");
39-
breadcrumb.setMessage(msg);
40-
breadcrumb.setLevel(level);
41-
if (tag != null) {
42-
breadcrumb.setData("tag", tag);
52+
if (Boolean.TRUE.equals(isAddingBreadcrumb.get())) {
53+
return;
4354
}
44-
if (tr != null && tr.getMessage() != null) {
45-
breadcrumb.setData("throwable", tr.getMessage());
55+
isAddingBreadcrumb.set(Boolean.TRUE);
56+
try {
57+
Breadcrumb breadcrumb = new Breadcrumb();
58+
breadcrumb.setCategory("Logcat");
59+
breadcrumb.setMessage(msg);
60+
breadcrumb.setLevel(level);
61+
if (tag != null) {
62+
breadcrumb.setData("tag", tag);
63+
}
64+
if (tr != null && tr.getMessage() != null) {
65+
breadcrumb.setData("throwable", tr.getMessage());
66+
}
67+
Sentry.addBreadcrumb(breadcrumb);
68+
} finally {
69+
isAddingBreadcrumb.set(Boolean.FALSE);
4670
}
47-
Sentry.addBreadcrumb(breadcrumb);
4871
}
4972

5073
private static void addAsLog(

0 commit comments

Comments
 (0)