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

Crash in serialization code on iOS #225

Closed
yuriry opened this issue Sep 28, 2018 · 6 comments
Closed

Crash in serialization code on iOS #225

yuriry opened this issue Sep 28, 2018 · 6 comments

Comments

@yuriry
Copy link

yuriry commented Sep 28, 2018

My serializable classes don't use unsupported native features - no enums, arrays, etc. They only use Lists, Strings and Ints. Android project has no problem serializing/deserializaing the classes. Native project for iOS compiles without errors. But as soon as I try to deserialize a string on iOS, I get the error below. I copied the string that crashes deserialization on iOS and tested it using JUnit in my Android library and it deserializes without problems.

kfun:kotlin.Error.<init>(kotlin.String?)kotlin.Error + 70
kfun:kotlin.NotImplementedError.<init>(kotlin.String)kotlin.NotImplementedError + 70
kfun:[email protected]<#GENERIC_kotlin.Any>.()Generic + 211
kfun:[email protected]<#GENERIC_kotlin.Any>.()Generic + 84
kfun:kotlinx.serialization.context.getOrDefault@kotlinx.serialization.context.SerialContext?.(kotlin.reflect.KClass<#GENERIC_kotlin.Any>)Generic + 179
kfun:Foo.Companion.parse(kotlin.String)Foo + 224

Foo.parse() is the method that I call from iOS and Android:

@Serializable
data class Foo(@SerialName("Bar") val Bars: List<Bar>? = null) {
    
    @Serializable
    data class Bar(...)

    companion object {
        @JvmStatic
        fun parse(jsonText: String): Foo = JSON.nonstrict.parse(jsonText)
    }
}

Am I doing anything wrong here?

Thank you in advance!

@yuriry
Copy link
Author

yuriry commented Sep 28, 2018

More info: if I pass serializer to JSON.parse() method as below

@Serializable
data class Foo(@SerialName("Bar") val Bars: List<Bar>? = null) {
    
    @Serializable
    data class Bar(...)

    companion object {
        @JvmStatic
        fun parse(jsonText: String): Foo = JSON.nonstrict.parse(serializer(), jsonText)
    }
}

I get the following error

kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception + 70
kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException + 70
kfun:kotlin.native.IncorrectDereferenceException.<init>(kotlin.String)kotlin.native.IncorrectDereferenceException + 70
ThrowIncorrectDereferenceException + 66
CheckIsMainThread + 26
kfun:kotlinx.serialization.json.<get-C2TC>#internal + 15
kfun:kotlinx.serialization.json.charToTokenClass$kotlinx-serialization-runtime-native(kotlin.Char)ValueType + 67
kfun:kotlinx.serialization.json.Parser.nextToken() + 159
kfun:kotlinx.serialization.json.Parser.<init>(kotlin.String)kotlinx.serialization.json.Parser + 267
kfun:kotlinx.serialization.json.JSON.parse(kotlinx.serialization.DeserializationStrategy<#GENERIC>;kotlin.String)Generic + 162
kfun:Foo.Companion.parse(kotlin.String)Foo + 174

Is the error related to Main Thread? (CheckIsMainThread)

@sandwwraith
Copy link
Member

Hi, for the first exception I can say that it is not working by design – since there are no reflection on K/N, KClass.serializer() method throws NotImplementedError and therefore, you should always use two-arguments JSON.stringify and JSON.parse, passing a serializer alongside the object.

Second issue is likely indeed connected with threading. From which thread you're trying to use a parser? K/N has limitations on access to global constants from different threads.

@sandwwraith
Copy link
Member

It is indeed connected to recent changes in K/N mutability policy. Now global vals are only accessible from the main thread (see https:/JetBrains/kotlin-native/blob/master/IMMUTABILITY.md) for details. I'll make a workaround.

@yuriry
Copy link
Author

yuriry commented Sep 28, 2018

@sandwwraith Thank you for the information! I'm indeed deserializing on a worker thread. Will be looking forward to the workaround to resume my project. Please update the thread when it is available. Thank you again.

sandwwraith added a commit that referenced this issue Sep 28, 2018
…ser, so it is now accessible from multiple workers

Fixes #225
@yuriry
Copy link
Author

yuriry commented Sep 30, 2018

@sandwwraith Thank you for the quick fix! I can confirm that the problem is resolved in eap13 branch.

@sandwwraith
Copy link
Member

Fixed in 0.8.2-rc13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants