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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eagerly load CoroutineExceptionHandler and load corresponding service #2957

Merged
merged 2 commits into from Oct 12, 2021

Conversation

qwwdfsad
Copy link
Member

Partially addresses #2552

@qwwdfsad qwwdfsad marked this pull request as ready for review September 29, 2021 11:47
kotlinx-coroutines-core/common/src/Job.kt Show resolved Hide resolved
@@ -22,6 +22,12 @@ private val handlers: List<CoroutineExceptionHandler> = ServiceLoader.load(
CoroutineExceptionHandler::class.java.classLoader
).iterator().asSequence().toList()

internal actual fun initializeDefaultExceptionHandlers() {
// Load CEH and handlers
CoroutineExceptionHandler
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd expect Class.forName or something here instead. As is, could you please explain due to what guarantees does this not get thrown away as dead code?

Copy link
Member Author

Choose a reason for hiding this comment

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

JVM has no right to throw away any code with side effects. Classloading is not pure, GETSTATIC CEH.Key will trigger classloading:

https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html#jvms-5.4.3

Also, the key here is to initialize handlers which are initialized when initializeDefaultExceptionHandlers first accessed (because these symbols belong to the same class)

@qwwdfsad qwwdfsad merged commit 0b65246 into develop Oct 12, 2021
@qwwdfsad qwwdfsad deleted the better-exception-handling branch October 12, 2021 16:05
yorickhenning pushed a commit to yorickhenning/kotlinx.coroutines that referenced this pull request Oct 14, 2021
…Kotlin#2957)

* Eagerly load CoroutineExceptionHandler and load the corresponding service

Partially addresses Kotlin#2552
qwwdfsad added a commit that referenced this pull request Feb 16, 2022
qwwdfsad added a commit that referenced this pull request Feb 17, 2022
dee-tree pushed a commit to dee-tree/kotlinx.coroutines that referenced this pull request Jul 21, 2022
pablobaxter pushed a commit to pablobaxter/kotlinx.coroutines that referenced this pull request Sep 14, 2022
…Kotlin#2957)

* Eagerly load CoroutineExceptionHandler and load the corresponding service

Partially addresses Kotlin#2552
pablobaxter pushed a commit to pablobaxter/kotlinx.coroutines that referenced this pull request Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants