Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sentry recovers after a Thread had currentHub set to a NoOpHub #2076

Merged
merged 2 commits into from Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -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
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
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