Search code examples
ioskotlinworkerkotlin-multiplatformkotlin-native

kotlin-native freeze() method crashing when freezing a function


I am trying to freeze a function to be able to pass to a worker. In the same class as the function I have a variable which is marked with ensureNeverFrozen. This variable is not used in the function I am trying to freeze, in fact, the function does not do anything.

Still, when I try to freeze the function, I get the below error

Minimal crash reproducing code -

Instances of kotlin.Error, kotlin.RuntimeException and subclasses aren't propagated from Kotlin to Objective-C/Swift.
Other exceptions can be propagated as NSError if method has or inherits @Throws annotation.
Uncaught Kotlin exception: kotlin.native.concurrent.FreezingException: freezing of function performWork (Kotlin reflection is not available) has failed, first blocker is []
        at 0   lib                                 0x0000000102502a45 kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception + 85
        at 1   lib                                 0x0000000102502125 kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException + 85
        at 2   lib                                 0x00000001025265c1 kfun:kotlin.native.concurrent.FreezingException.<init>(kotlin.Any;kotlin.Any)kotlin.native.concurrent.FreezingException + 641
        at 3   lib                                 0x0000000102526a2e ThrowFreezingException + 222
        at 4   lib                                 0x0000000102575833 FreezeSubgraph + 2611
        at 5   lib                                 0x000000010258bcfb Kotlin_Worker_freezeInternal + 27
        at 6   lib                                 0x000000010252673b kfun:kotlin.native.concurrent.freeze@#GENERIC.()Generic + 59
        at 7   lib                                 0x000000010253e310 kfun:com.prateekgrover.lib.KNDispatchQueue.performWorkAsync() + 208
        at 8   lib                                 0x000000010253de1a kfun:com.prateekgrover.lib.Greeting.start() + 186
        at 9   lib                                 0x000000010254168d blockCopyHelper + 573
        at 10  ios_kn_sample                       0x000000010215471d $sIeg_IeyB_TR + 45 (/Users/prateek.grover/Documents/code/KotlinNative/KotlinNativeSample/core_lib_dup/ios_kn_sample/<compiler-generated>:<unknown>)
        at 11  libdispatch.dylib                   0x0000000104156d7f _dispatch_call_block_and_release + 12
        at 12  libdispatch.dylib                   0x0000000104157db5 _dispatch_client_callout + 8
        at 13  libdispatch.dylib                   0x000000010415a7b9 _dispatch_queue_override_invoke + 1022
        at 14  libdispatch.dylib                   0x0000000104168632 _dispatch_root_queue_drain + 351
        at 15  libdispatch.dylib                   0x0000000104168fca _dispatch_worker_thread2 + 130
        at 16  libsystem_pthread.dylib             0x00000001045406b3 _pthread_wqthread + 583
        at 17  libsystem_pthread.dylib             0x00000001045403fd start_wqthread + 13

Any explanation for the above behaviour?


Solution

  • I assume you're referring to this

    actual class KNDispatchQueue {
        @ThreadLocal
        actual companion object Singleton {
            actual val sharedInstance: KNDispatchQueue = KNDispatchQueue()
        }
    
        private var workQueue: MutableList<KNWork> = mutableListOf()
    
        init {
            workQueue.ensureNeverFrozen()
        }
    
        actual fun performWorkAsync() {
            ::performWork.freeze()
        }
    
        fun performWork() {
    
        }
    }
    

    The function is part of the class. Assuming it tries to freeze that instance of itself, it's also freezing the rest of the graph. I commented out ensureNeverFrozen and it freezes.

    Your syntax ::performWork is pointing at the instance ref.

    https://kotlinlang.org/docs/reference/reflection.html#bound-function-and-property-references-since-11