diff --git a/CHANGELOG.md b/CHANGELOG.md index 648533ec18..93cd3765da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ ### Fixes +- Fix attachments being duplicated on native events that carry scope attachments ([#5548](https://github.com/getsentry/sentry-java/pull/5548)) - Fix performance collector scheduling many tasks in a row ([#5524](https://github.com/getsentry/sentry-java/pull/5524)) ## 8.43.2 diff --git a/sentry/src/main/java/io/sentry/SentryClient.java b/sentry/src/main/java/io/sentry/SentryClient.java index 5ac81c4493..78225f05d1 100644 --- a/sentry/src/main/java/io/sentry/SentryClient.java +++ b/sentry/src/main/java/io/sentry/SentryClient.java @@ -112,7 +112,9 @@ private boolean shouldApplyScopeData(final @NotNull CheckIn event, final @NotNul hint = new Hint(); } - if (shouldApplyScopeData(event, hint)) { + // Cached envelopes (e.g. native crashes from the outbox) already carry their attachments as + // envelope items. Re-applying scope attachments here would duplicate them. + if (shouldApplyScopeData(event, hint) && !HintUtils.hasType(hint, Cached.class)) { addScopeAttachmentsToHint(scope, hint); } diff --git a/sentry/src/test/java/io/sentry/SentryClientTest.kt b/sentry/src/test/java/io/sentry/SentryClientTest.kt index d5b2f0f82a..ab6fd2075a 100644 --- a/sentry/src/test/java/io/sentry/SentryClientTest.kt +++ b/sentry/src/test/java/io/sentry/SentryClientTest.kt @@ -870,6 +870,25 @@ class SentryClientTest { assertEquals(scope.level, event.level) } + @Test + fun `when hint is Cached, scope attachments are not added to avoid duplication`() { + val sut = fixture.getSut() + + val event = createEvent() + val scope = createScopeWithAttachments() + + val hints = HintUtils.createWithTypeCheckHint(CustomCachedApplyScopeDataHint()) + sut.captureEvent(event, scope, hints) + + verify(fixture.transport) + .send( + check { actual -> + assertEquals(0, actual.items.count { it.header.type == SentryItemType.Attachment }) + }, + anyOrNull(), + ) + } + @Test fun `when transport factory is NoOp, it should initialize it`() { fixture.sentryOptions.setTransportFactory(NoOpTransportFactory.getInstance())