'mutation attempt of frozen io.ktor.client.request.HttpRequestPipeline@151ac8
I'm using IO Ktor: ktor client core to implement multi platform network requests, but I encountered some problems
When I use the class attribute created by httpclient () and the local variable created by httpclient () in the method to call the request interface respectively, the first method will report an error. I don't know what the problem is. If you can, please help me
Exception doesn't match @Throws-specified class list and thus isn't propagated from Kotlin to Objective-C/Swift as NSError.
It is considered unexpected and unhandled instead. Program will be terminated.
Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen io.ktor.client.request.HttpRequestPipeline@151ac8
at 0 KmmiOS 0x0000000102ba7fe4 ThrowInvalidMutabilityException + 200
at 1 KmmiOS 0x0000000102ca7390 kfun:io.ktor.util.pipeline.Pipeline.notSharedInterceptorsList#internal + 216
at 2 KmmiOS 0x0000000102ca6e90 kfun:io.ktor.util.pipeline.Pipeline.sharedInterceptorsList#internal + 1360
at 3 KmmiOS 0x0000000102ca3980 kfun:io.ktor.util.pipeline.Pipeline#execute(1:1;1:0){}1:0 + 388
at 4 KmmiOS 0x0000000102cdc5a4 kfun:io.ktor.client.HttpClient.$executeCOROUTINE$19#invokeSuspend(kotlin.Result<kotlin.Any?>){}kotlin.Any? + 428
at 5 KmmiOS 0x0000000102d0d7a4 kfun:io.ktor.client.statement.HttpStatement.$executeUnsafeCOROUTINE$64#invokeSuspend(kotlin.Result<kotlin.Any?>){}kotlin.Any? + 884
at 6 KmmiOS 0x0000000102d0ca58 kfun:io.ktor.client.statement.HttpStatement.$executeCOROUTINE$61#invokeSuspend(kotlin.Result<kotlin.Any?>){}kotlin.Any? + 532
at 7 KmmiOS 0x0000000102d0d378 kfun:io.ktor.client.statement.HttpStatement#execute(){}io.ktor.client.statement.HttpResponse + 392
at 8 KmmiOS 0x0000000102d77a2c kfun:com.example.kmmnetworkmodule2.CMTKmmNetwork.$getHtmlCOROUTINE$0#invokeSuspend(kotlin.Result<kotlin.Any?>){}kotlin.Any? + 1404
at 9 KmmiOS 0x0000000102e1b984 objc2kotlin.1062 + 320
at 10 KmmiOS 0x0000000102b86754 $s6KmmiOS14ViewControllerC11viewDidLoadyyF + 1000
at 11 KmmiOS 0x0000000102b86b70 $s6KmmiOS14ViewControllerC11viewDidLoadyyFTo + 36
at 12 UIKitCore 0x0000000184641568 -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 104
at 13 UIKitCore 0x0000000184645bb8 -[UIViewController loadViewIfRequired] + 1020
at 14 UIKitCore 0x0000000184645f84 -[UIViewController view] + 28
at 15 UIKitCore 0x0000000184dd4904 -[UIWindow addRootViewControllerViewIfPossible] + 176
at 16 UIKitCore 0x0000000184dd4114 -[UIWindow _updateLayerOrderingAndSetLayerHidden:actionBlock:] + 224
at 17 UIKitCore 0x0000000184dd4f64 -[UIWindow _setHidden:forced:] + 252
at 18 UIKitCore 0x0000000184de5b00 -[UIWindow _mainQueue_makeKeyAndVisible] + 52
at 19 UIKitCore 0x000000018502ca7c -[UIWindowScene _makeKeyAndVisibleIfNeeded] + 200
at 20 UIKitCore 0x00000001841daafc +[UIScene _sceneForFBSScene:create:withSession:connectionOptions:] + 1428
at 21 UIKitCore 0x0000000184d950ec -[UIApplication _connectUISceneFromFBSScene:transitionContext:] + 1280
at 22 UIKitCore 0x0000000184d95588 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 300
at 23 UIKitCore 0x0000000184869904 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 360
at 24 FrontBoardServices 0x000000018614ba84 -[FBSScene _callOutQueue_agent_didCreateWithTransitionContext:completion:] + 412
at 25 FrontBoardServices 0x0000000186177cec __94-[FBSWorkspaceScenesClient createWithSceneID:groupID:parameters:transitionContext:completion:]_block_invoke.180 + 100
at 26 FrontBoardServices 0x000000018615a160 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 232
at 27 FrontBoardServices 0x0000000186177908 __94-[FBSWorkspaceScenesClient createWithSceneID:groupID:parameters:transitionContext:completion:]_block_invoke + 312
at 28 libdispatch.dylib 0x00000001033f1694 _dispatch_client_callout + 16
dyld4 config: DYLD_ROOT_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot DYLD_LIBRARY_PATH=/Users/2yue8hao/Library/Developer/Xcode/DerivedData/KmmiOS-gbbrsiyqftkmsugfobpnraelllac/Build/Products/Debug-iphonesimulator:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libBacktraceRecording.dylib:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libMainThreadChecker.dylib:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib DYLD_FRAMEWORK_PATH=/Users/2yue8hao/Library/Developer/Xcode/DerivedData/KmmiOS-gbbrsiyqftkmsugfobpnraelllac/Build/Products/Debug-iphonesimulator
CoreSimulator 783.5 - Device: iPhone 13 (47D37E06-9A22-47D8-AAB4-F4FB50DCD7DF) - Runtime: iOS 15.2 (19C51) - DeviceType: iPhone 13
My dependence:
val commonMain by getting {
dependencies {
implementation("io.ktor:ktor-client-core:2.0.0")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
}
}
If I call like this,The above error will be displayed
object CMTKmmNetwork {
private var client: HttpClient = HttpClient()
suspend fun getHtml(): String {
val response = client.get("https://ktor.io/docs")
return response.bodyAsText()
}
}
But if so
object CMTKmmNetwork {
suspend fun getHtml(): String {
val response = HttpClient().get("https://ktor.io/docs")
return response.bodyAsText()
}
}
is ok
Call mode in iOS
import kmmnetworkmodule2
class ViewController: UIViewController {
@IBOutlet weak var contentLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
CMTKmmNetwork.shared.getHtml { v1, v2 in
print(v1)
}
}
}
Solution 1:[1]
This seems to be a bug in ktor 2.0
As a workaround, you can use the new memory model (at least this fixed the issue in my project): https://github.com/JetBrains/kotlin/blob/master/kotlin-native/NEW_MM.md#enable-the-new-mm
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ydemartino |