最近几天我一直在与这个异常作斗争。
我有一个kotlin多平台项目,其中包含以下依赖项:
- kotlin=1.5.10
- kodein=7.6.0
- ktor=1.6.0(内部使用kotlin协程1.5.0-native-mt(
我在尝试在本机中使用httpClient时遇到了异常:
at kotlin.Throwable#<init>(/Users/teamcity3/buildAgent/work/290aee0e088a1666/runtime/src/main/kotlin/kotlin/Throwable.kt:23)
at kotlin.Exception#<init>(/Users/teamcity3/buildAgent/work/290aee0e088a1666/runtime/src/main/kotlin/kotlin/Exceptions.kt:23)
at kotlin.RuntimeException#<init>(/Users/teamcity3/buildAgent/work/290aee0e088a1666/runtime/src/main/kotlin/kotlin/Exceptions.kt:34)
at kotlin.native.concurrent.InvalidMutabilityException#<init>(/Users/teamcity3/buildAgent/work/290aee0e088a1666/runtime/src/main/kotlin/kotlin/native/concurrent/Freezing.kt:22)
at <global>.ThrowInvalidMutabilityException(/Users/teamcity3/buildAgent/work/290aee0e088a1666/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt:93)
at <global>.MutationCheck(Unknown Source)
at kotlin.collections.HashMap.<set-length>#internal(/Users/teamcity3/buildAgent/work/290aee0e088a1666/runtime/src/main/kotlin/kotlin/collections/HashMap.kt:16)
at kotlin.collections.HashMap#addKey(/Users/teamcity3/buildAgent/work/290aee0e088a1666/runtime/src/main/kotlin/kotlin/collections/HashMap.kt:292)
at kotlin.collections.HashMap#put(/Users/teamcity3/buildAgent/work/290aee0e088a1666/runtime/src/main/kotlin/kotlin/collections/HashMap.kt:68)
at org.kodein.di.internal.DITreeImpl#find(/Users/runner/work/Kodein-DI/Kodein-DI/kodein-di/src/commonMain/kotlin/org/kodein/di/internal/DITreeImpl.kt:132)
at org.kodein.di.DITree#find$default(/Users/runner/work/Kodein-DI/Kodein-DI/kodein-di/src/commonMain/kotlin/org/kodein/di/DITree.kt:36)
at org.kodein.di.internal.DIContainerImpl#factory(/Users/runner/work/Kodein-DI/Kodein-DI/kodein-di/src/commonMain/kotlin/org/kodein/di/internal/DIContainerImpl.kt:158)
at org.kodein.di.DIContainer#factory$default(/Users/runner/work/Kodein-DI/Kodein-DI/kodein-di/src/commonMain/kotlin/org/kodein/di/DIContainer.kt:32)
at org.kodein.di.DIContainer#provider(/Users/runner/work/Kodein-DI/Kodein-DI/kodein-di/src/commonMain/kotlin/org/kodein/di/DIContainer.kt:76)
at org.kodein.di.DIContainer#provider$default(/Users/runner/work/Kodein-DI/Kodein-DI/kodein-di/src/commonMain/kotlin/org/kodein/di/DIContainer.kt:75)
at org.kodein.di.internal.DirectDIBaseImpl#Instance(/Users/runner/work/Kodein-DI/Kodein-DI/kodein-di/src/commonMain/kotlin/org/kodein/di/internal/DirectDIImpl.kt:30)
at InvalidMutabilitySampleTest.<init>$lambda-6$lambda-5$lambda-4$lambda-3$lambda-2#internal(/Users/r.juszczyk/StudioProjects/lmhu-multiplatform-app/MultiplatformApp/src/iosTest/kotlin/InvalidMutabilitySampleTest.kt:26)
at InvalidMutabilitySampleTest.$<init>$lambda-6$lambda-5$lambda-4$lambda-3$lambda-2$FUNCTION_REFERENCE$3.invoke#internal(/Users/r.juszczyk/StudioProjects/lmhu-multiplatform-app/MultiplatformApp/src/iosTest/kotlin/InvalidMutabilitySampleTest.kt:25)
at InvalidMutabilitySampleTest.$<init>$lambda-6$lambda-5$lambda-4$lambda-3$lambda-2$FUNCTION_REFERENCE$3.$<bridge-UNNN>invoke(/Users/r.juszczyk/StudioProjects/lmhu-multiplatform-app/MultiplatformApp/src/iosTest/kotlin/InvalidMutabilitySampleTest.kt:25)
at io.ktor.client.HttpClientConfig.install$<anonymous>_1#internal(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClientConfig.kt:69)
at io.ktor.client.HttpClientConfig.$install$<anonymous>_1$FUNCTION_REFERENCE$17.invoke#internal(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClientConfig.kt:65)
at io.ktor.client.HttpClientConfig.$install$<anonymous>_1$FUNCTION_REFERENCE$17.$<bridge-UNNN>invoke(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClientConfig.kt:65)
at io.ktor.client.features.json.JsonFeature.Feature#prepare(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-features/ktor-client-json/common/src/io/ktor/client/features/json/JsonFeature.kt:129)
at io.ktor.client.HttpClientConfig.install$<anonymous>_1-2#internal(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClientConfig.kt:77)
at io.ktor.client.HttpClientConfig.$install$<anonymous>_1-2$FUNCTION_REFERENCE$18.invoke#internal(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClientConfig.kt:74)
at io.ktor.client.HttpClientConfig.$install$<anonymous>_1-2$FUNCTION_REFERENCE$18.$<bridge-UNNN>invoke(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClientConfig.kt:74)
at io.ktor.client.HttpClientConfig#install(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClientConfig.kt:97)
at io.ktor.client.HttpClient#<init>(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt:172)
at io.ktor.client.HttpClient#<init>(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt:81)
at io.ktor.client#HttpClient(/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt:43)
我设法在一次测试中重现了那次崩溃:
class InvalidMutabilitySampleTest {
val di = DI {
import(DI.Module("Some Module") {
bind<Json>() with provider {
Json {
prettyPrint = true
isLenient = true
}
}
bind<HttpClient>() with provider {
HttpClient{
install(JsonFeature) {
serializer = KotlinxSerializer(instance())
}
}
}
})
}
val httpClient: HttpClient by di.instance()
@Test
fun invalidMutabilityTest() {
println(httpClient)
}
}
我还设法通过更改来修复它:
HttpClient{
install(JsonFeature) {
serializer = KotlinxSerializer(instance())
}
}
至:
val json = instance<Json>()
HttpClient{
install(JsonFeature) {
serializer = KotlinxSerializer(json)
}
}
然后我注意到HttpClient做了一件非常具体的事情——它在init块中冻结自己。我设法用这个样本代码复制了它:
class FrozenConstructor(val block: ()->Unit) {
init {
freeze()
}
}
class InvalidMutabilitySampleTest2 {
val di = DI {
import(DI.Module("Some Module") {
bind<String>() with provider {
"lolo"
}
bind<FrozenConstructor>() with provider {
FrozenConstructor{
instance<String>()
}
}
})
}
val frozenConstructor: FrozenConstructor by di.instance()
@Test
fun invalidMutabilityTest() {
println(frozenConstructor)
}
}
所以我的理论是:
- kodein尝试提供
FrozenConstructor
类 - 创建了
FrozenConstructor
,它冻结了自身及其引用kodein的成员 - kodein尝试缓存提供的依赖项,并尝试对内部
MutableMap
进行突变,后者被冻结,一切都崩溃了
有人能确认这或多或少是正确的吗?如果没有,可以纠正我吗?
你们能不能提出最好的处理方法,以及其他正在等待的陷阱?
这是科德因病毒吗?
如果我使用的是with provider
而不是with singleton
,为什么kodein必须在可变映射中存储一些东西?
我想你已经大致弄明白了。Ktor冻结自身及其所有配置,以确保它可以在Kotlin/Native中跨线程使用。Kotein认为你只会从一根线接触它,冷冻是不安全的。(无论是限制、错误还是设计缺陷,都可能有待解释。(
为了解决这些问题,您希望避免在HttpClient
配置中意外捕获引用Kodein内部的this
引用。一个很好的方法是从HttpClient
lambda之外的辅助变量中的DI获取实例,正如您所发现的那样。
你是对的。FrozenConstructor
冻结了它自己,因此它的block
属性,作为lambda,它冻结了它的所有捕获,其中包括DI容器。
Kotein DI不支持冻结(因此不支持本地多线程(。自从JB宣布开发新的GC以来,我们决定不投资使其与原生多线程兼容。