Skip to content

Commit

Permalink
Fix UserFeedback disk cache name conflicts with linked events (#3116)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
markushi authored Jan 26, 2024
1 parent 0816a48 commit 7ab32b6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 4 additions & 9 deletions sentry/src/main/java/io/sentry/cache/EnvelopeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
30 changes: 30 additions & 0 deletions sentry/src/test/java/io/sentry/cache/EnvelopeCacheTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 7ab32b6

Please sign in to comment.