Skip to content

Commit

Permalink
Sentry recovers after a Thread had currentHub set to a NoOpHub (#2076)
Browse files Browse the repository at this point in the history
  • Loading branch information
adinauer committed Jun 4, 2022
1 parent 712115b commit fd2d94e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Sentry can now self heal after a Thread had its currentHub set to a NoOpHub ([#2076](https://github.com/getsentry/sentry-java/pull/2076))

## 6.0.0-rc.1

### Features
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private Sentry() {}
return mainHub;
}
IHub hub = currentHub.get();
if (hub == null) {
if (hub == null || hub instanceof NoOpHub) {
hub = mainHub.clone();
currentHub.set(hub);
}
Expand Down
26 changes: 25 additions & 1 deletion sentry/src/test/java/io/sentry/SentryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class SentryTest {

private val dsn = "http://key@localhost/proj"
Expand Down Expand Up @@ -209,6 +209,30 @@ class SentryTest {
assertFalse(File(sentryOptions?.profilingTracesDirPath!!).exists())
}

@Test
fun `using sentry before calling init creates NoOpHub but after init Sentry uses a new clone`() {
// noop as not yet initialized, caches NoOpHub in ThreadLocal
Sentry.captureMessage("noop caused")

assertTrue(Sentry.getCurrentHub() is NoOpHub)

// init Sentry in another thread
val thread = Thread() {
Sentry.init {
it.dsn = dsn
it.isDebug = true
}
}
thread.start()
thread.join()

Sentry.captureMessage("should work now")

val hub = Sentry.getCurrentHub()
assertNotNull(hub)
assertFalse(hub is NoOpHub)
}

private fun getTempPath(): String {
val tempFile = Files.createTempDirectory("cache").toFile()
tempFile.delete()
Expand Down

0 comments on commit fd2d94e

Please sign in to comment.