-
-
Notifications
You must be signed in to change notification settings - Fork 472
perf(core): [Init Reflection 1] Probe class availability without initializing #5635
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
runningcode
wants to merge
3
commits into
no/perf-init-reflection
Choose a base branch
from
no/perf-init-reflection-no-init
base: no/perf-init-reflection
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+58
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
SentryNdkthe static init block will now run much later, potentially causing ANRs.Previously
Class.forNametriggeredstatic blockearly and gave it time to runSentryNdk.loadNativeLibraries()in the background before we waited forloadLibraryLatch.awaitin theinitmethod.Now this may cause applications to spend more time waiting on main thread. Worst case would be 2s additional wait on main thread.
There was a problem hiding this comment.
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
ThrowableinSpanFactoryFactoryandScopesStorageFactoryinstead 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/falsefor theClass.forNamecall from the caller.This however would increase complexity of caching results since we might have invoked
Class.forNamewithfalsealready and cached the result, then another caller might wanttruebut since we already cached it it won't do it.