Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Performance

- Probe class availability without initializing the class during SDK init ([#5635](https://github.com/getsentry/sentry-java/pull/5635))

## 8.45.0

### Features
Expand Down
4 changes: 3 additions & 1 deletion sentry/src/main/java/io/sentry/util/LoadClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public class LoadClass {
*/
public @Nullable Class<?> loadClass(final @NotNull String clazz, final @Nullable ILogger logger) {
try {
return Class.forName(clazz);
// Don't initialize the class just to probe for availability; it gets initialized lazily on
// first use. This avoids running unrelated static initializers during SDK init.
return Class.forName(clazz, false, LoadClass.class.getClassLoader());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In SentryNdk the static init block will now run much later, potentially causing ANRs.

Previously Class.forName triggered static block early and gave it time to run SentryNdk.loadNativeLibraries() in the background before we waited for loadLibraryLatch.await in the init method.

Now this may cause applications to spend more time waiting on main thread. Worst case would be 2s additional wait on main thread.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also leads to a very theoretical case of classes being handed back that then fail when creating an instance. This could bite us on the OTel span factory and scopes storage. One fix here could be to just catch Throwable in SpanFactoryFactory and ScopesStorageFactory instead of all the individual exceptions where we might then miss ExceptionInInitializerError/NoClassDefFoundError/LinkageError.

But looking at the finding above, it may make sense to allow controlling true/false for the Class.forName call from the caller.

This however would increase complexity of caching results since we might have invoked Class.forName with false already and cached the result, then another caller might want true but since we already cached it it won't do it.

} catch (ClassNotFoundException e) {
if (logger != null) {
logger.log(SentryLevel.INFO, "Class not available: " + clazz);
Expand Down
49 changes: 49 additions & 0 deletions sentry/src/test/java/io/sentry/util/LoadClassTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.sentry.util

import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull

class LoadClassTest {
@Test
fun `loadClass returns the class when it is available`() {
assertNotNull(LoadClass().loadClass("io.sentry.SentryEvent", null))
}

@Test
fun `loadClass returns null when the class is not available`() {
assertNull(LoadClass().loadClass("io.sentry.ThisClassDoesNotExist", null))
}

@Test
fun `isClassAvailable reflects whether the class is on the classpath`() {
val loadClass = LoadClass()
assertNotNull(loadClass.loadClass("io.sentry.SentryEvent", null))
assertFalse(
loadClass.isClassAvailable("io.sentry.ThisClassDoesNotExist", null as io.sentry.ILogger?)
)
}

@Test
fun `loadClass does not run the static initializer of the probed class`() {
// Reading the flag initializes the flag holder, not the probe.
assertFalse(LoadClassNoInitFlag.initialized)

// Obtaining the name via ::class.java does not initialize the probe either.
LoadClass().loadClass(LoadClassNoInitProbe::class.java.name, null)

// Availability probing must not trigger the probe's static initializer.
assertFalse(LoadClassNoInitFlag.initialized)
}
}

private object LoadClassNoInitFlag {
@JvmField var initialized = false
}

private object LoadClassNoInitProbe {
init {
LoadClassNoInitFlag.initialized = true
}
}
Loading