Skip to content

Commit bed7062

Browse files
committed
Review
1 parent 434da93 commit bed7062

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

kotlinx-coroutines-core/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ fun setupManifest(jar: Jar) {
167167
jar.manifest {
168168
attributes(
169169
mapOf(
170-
"Premain-Class" to "kotlinx.coroutines.debug.internal.SafeAgentPremain",
170+
"Premain-Class" to "kotlinx.coroutines.debug.internal.AgentPremain",
171171
"Can-Retransform-Classes" to "true",
172172
)
173173
)

kotlinx-coroutines-core/jvm/src/debug/internal/AgentPremain.kt

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ import java.security.*
1515
@SuppressLint("all")
1616
@IgnoreJRERequirement // Never touched on Android
1717
internal object AgentPremain {
18+
// This should be the first property to ensure the check happens first! Add new properties only below!
19+
private val dummy = checkIfStdlibIsAvailable()
20+
21+
/**
22+
* This check ensures that kotlin-stdlib classes are loaded by the time AgentPremain is initialized;
23+
* otherwise the debug session would fail with `java.lang.NoClassDefFoundError: kotlin/Result`.
24+
*/
25+
private fun checkIfStdlibIsAvailable() {
26+
try {
27+
Result.success(42)
28+
} catch (t: Throwable) {
29+
error("kotlinx.coroutines debug agent failed to load.\n" +
30+
"Please ensure that the Kotlin standard library is present in the classpath.\n" +
31+
"Alternatively, you can disable kotlinx.coroutines debug agent by removing `-javaagent=/path/kotlinx-coroutines-core.jar` from your VM arguments.\n" +
32+
t.cause)
33+
}
34+
}
1835

1936
private val enableCreationStackTraces = runCatching {
2037
System.getProperty("kotlinx.coroutines.debug.enable.creation.stack.trace")?.toBoolean()
@@ -69,25 +86,3 @@ internal object AgentPremain {
6986
}
7087
}
7188
}
72-
73-
/**
74-
* This class is loaded if and only if kotlinx-coroutines-core was used as a javaagent argument.
75-
* It serves as a "safe" wrapper around [AgentPremain] that does not require any kotlin-stdlib classes to be loaded for its initialization.
76-
*
77-
* Initialization of [AgentPremain] may fail, e.g., because it started before the required kotlin-stdlib classes were loaded.
78-
* If `kotlinx.coroutines.apply.debug.agent` is set to `false`, the coroutine debug agent will not be applied.
79-
*/
80-
@Suppress("unused")
81-
internal object SafeAgentPremain {
82-
@JvmStatic
83-
fun premain(args: String?, instrumentation: Instrumentation) {
84-
try {
85-
AgentPremain.premain(args, instrumentation)
86-
} catch (t: Throwable) {
87-
error("Something went wrong while loading kotlinx.coroutines debug agent.\n" +
88-
"Please ensure that Kotlin Stdlib is present in the classpath.\n" +
89-
"Alternatively, to disable coroutines debug agent you can remove `-javaagent=/path/kotlinx-coroutines-core.jar` from your VM arguments.\n" +
90-
t.cause)
91-
}
92-
}
93-
}

0 commit comments

Comments
 (0)