From 7ab32b6ab8bc72b973328a00840eb192b151b59f Mon Sep 17 00:00:00 2001 From: Markus Hintersteiner Date: Fri, 26 Jan 2024 10:47:10 -0500 Subject: [PATCH] Fix UserFeedback disk cache name conflicts with linked events (#3116) * Fix UserFeedback cache name conflicts with attached Event cache file name * Add Changelog * Remove uneccesary Exception * Fix method doc * Update CHANGELOG.md * Update CHANGELOG.md --- CHANGELOG.md | 1 + .../java/io/sentry/cache/EnvelopeCache.java | 13 +++----- .../java/io/sentry/cache/EnvelopeCacheTest.kt | 30 +++++++++++++++++++ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c1c719453..a41f81be0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Fix not eligible for auto proxying warnings ([#3154](https://github.com/getsentry/sentry-java/pull/3154)) - Set default fingerprint for ANRv2 events to correctly group background and foreground ANRs ([#3164](https://github.com/getsentry/sentry-java/pull/3164)) - This will improve grouping of ANRs that have similar stacktraces but differ in background vs foreground state. Only affects newly-ingested ANR events with `mechanism:AppExitInfo` +- Fix UserFeedback disk cache name conflicts with linked events ([#3116](https://github.com/getsentry/sentry-java/pull/3116)) ### Breaking changes diff --git a/sentry/src/main/java/io/sentry/cache/EnvelopeCache.java b/sentry/src/main/java/io/sentry/cache/EnvelopeCache.java index 5232d6077a..3be857a4b2 100644 --- a/sentry/src/main/java/io/sentry/cache/EnvelopeCache.java +++ b/sentry/src/main/java/io/sentry/cache/EnvelopeCache.java @@ -353,23 +353,18 @@ public void discard(final @NotNull SentryEnvelope envelope) { } /** - * Returns the envelope's file path. If the envelope has no eventId header, it generates a random - * file name to it. + * Returns the envelope's file path. If the envelope wasn't added to the cache beforehand, a + * random file name is assigned. * * @param envelope the SentryEnvelope object * @return the file */ private synchronized @NotNull File getEnvelopeFile(final @NotNull SentryEnvelope envelope) { - String fileName; + final @NotNull String fileName; if (fileNameMap.containsKey(envelope)) { fileName = fileNameMap.get(envelope); } else { - if (envelope.getHeader().getEventId() != null) { - fileName = envelope.getHeader().getEventId().toString(); - } else { - fileName = UUID.randomUUID().toString(); - } - fileName += SUFFIX_ENVELOPE_FILE; + fileName = UUID.randomUUID() + SUFFIX_ENVELOPE_FILE; fileNameMap.put(envelope, fileName); } diff --git a/sentry/src/test/java/io/sentry/cache/EnvelopeCacheTest.kt b/sentry/src/test/java/io/sentry/cache/EnvelopeCacheTest.kt index 260c6ae981..98751f8c44 100644 --- a/sentry/src/test/java/io/sentry/cache/EnvelopeCacheTest.kt +++ b/sentry/src/test/java/io/sentry/cache/EnvelopeCacheTest.kt @@ -1,6 +1,7 @@ package io.sentry.cache import io.sentry.DateUtils +import io.sentry.Hint import io.sentry.ILogger import io.sentry.NoOpLogger import io.sentry.SentryCrashLastRunState @@ -16,6 +17,7 @@ import io.sentry.cache.EnvelopeCache.SUFFIX_SESSION_FILE import io.sentry.hints.AbnormalExit import io.sentry.hints.SessionEndHint import io.sentry.hints.SessionStartHint +import io.sentry.protocol.SentryId import io.sentry.util.HintUtils import org.mockito.kotlin.mock import java.io.File @@ -317,4 +319,32 @@ class EnvelopeCacheTest { null ) } + + @Test + fun `two items with the same event id can be stored side-by-side`() { + val cache = fixture.getSUT() + + val eventId = SentryId() + + val envelopeA = SentryEnvelope.from( + fixture.options.serializer, + SentryEvent().apply { + setEventId(eventId) + }, + null + ) + + val envelopeB = SentryEnvelope.from( + fixture.options.serializer, + SentryEvent().apply { + setEventId(eventId) + }, + null + ) + + cache.store(envelopeA, Hint()) + cache.store(envelopeB, Hint()) + + assertEquals(2, cache.directory.list()?.size) + } }