JetBrains / kotlin-native

Kotlin/Native infrastructure
Apache License 2.0
7.02k stars 566 forks source link

crash on iOS if trying to iterate through maps #1873

Closed petrukhnov closed 6 years ago

petrukhnov commented 6 years ago
internal val myMap: Map<String, List<String>> = mapOf(
                "val1" to listOf("a1", "a2", "a3"),
                "val2" to listOf("b1", "b2")
        );

        fun getName(name: String): String {
            for (entry in myMap) {  //crash here
                if (entry.value.contains(name)) {
                    return entry.key;
                }
            }

            return name;
        }

Runnable example: https://github.com/JetBrains/kotlin-mpp-example/pull/5 App crashes immediately after start.

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: konan.worker.InvalidMutabilityException: mutation attempt of frozen {val1=[a1, a2, a3], val2=[b1, b2]} (hash is 0x20257639)
        at 3   Greeting                            0x000000010b02e216 kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception + 70
        at 4   Greeting                            0x000000010b02e136 kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException + 70
        at 5   Greeting                            0x000000010b05880d kfun:konan.worker.InvalidMutabilityException.<init>(kotlin.Any)konan.worker.InvalidMutabilityException + 285
        at 6   Greeting                            0x000000010b058925 ThrowInvalidMutabilityException + 85
        at 7   Greeting                            0x000000010b06b908 MutationCheck + 24
        at 8   Greeting                            0x000000010b049313 kfun:kotlin.collections.HashMap.<set-entriesView>#internal + 67
        at 9   Greeting                            0x000000010b048a2c private_functions_<stdlib>_kfun:kotlin.collections.HashMap.<get-entries>()kotlin.collections.MutableSet<kotlin.collections.MutableMap.MutableEntry<#GENERIC,#GENERIC>>_569 + 156
        at 10  Greeting                            0x000000010b02531a kfun:Calculator.Companion.getName(kotlin.String)kotlin.String + 298
        at 11  Greeting                            0x000000010b0259f4 kfun:org.greeting.Greeting.greetingCalc()kotlin.String + 212
        at 12  Greeting                            0x000000010b0258a4 kfun:Calculator.Companion.sum(kotlin.Int;kotlin.Int)ValueType + 548
        at 13  iosApp                              0x000000010ad3f984 _T06iosApp14ViewControllerC11viewDidLoadyyFTo + 36
        at 14  UIKit                               0x000000010c1aa131 -[UIViewController loadViewIfRequired] + 1215
        at 15  UIKit                               0x000000010c1aa574 -[UIViewController view] + 27
        at 16  UIKit                               0x000000010c078123 -[UIWindow addRootViewControllerViewIfPossible] + 122
        at 17  UIKit                               0x000000010c078834 -[UIWindow _setHidden:forced:] + 294
        at 18  UIKit                               0x000000010c08b5cc -[UIWindow makeKeyAndVisible] + 42
        at 19  UIKit                               0x000000010bfff3da -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4739
        at 20  UIKit                               0x000000010c0045cb -[UIApplication _runWithMainScene:transitionContext:completion:] + 1677
        at 21  UIKit                               0x000000010c3c6f7e __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 866
        at 22  UIKit                               0x000000010c799a39 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
        at 23  UIKit                               0x000000010c3c6bba -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236
        at 24  UIKit                               0x000000010c3c73db -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 675
        at 25  UIKit                               0x000000010cd38614 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 299
        at 26  UIKit                               0x000000010cd384ae -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433
        at 27  UIKit                               0x000000010ca1c75d __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 221
        at 28  UIKit                               0x000000010cc174b7 _performActionsWithDelayForTransitionContext + 100
        at 29  UIKit                               0x000000010ca1c627 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223
        at 30  UIKit                               0x000000010c7990e0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
        at 31  UIKit                               0x000000010c002eac -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 515
olonho commented 6 years ago

Seems you have a map inside a singleton object, which is frozen. This need to be fixed by rewrite of Map implementation in K/N stdlib.

petrukhnov commented 6 years ago

What could be good workaround?

olonho commented 6 years ago

For now, marking object with @ThreadLocal annotation, in general - we are discussing it.

olonho commented 6 years ago

Shall be fixed with 63d72d3bb358580ec1b51d25b7e327f3efd931b2

petrukhnov commented 6 years ago

Not fixed. Running with kotlin-native-gradle-plugin:0.8.2 give same error.

LouisCAD commented 6 years ago

@petrukhnov It is fixed on master, and will be fixed on next release. Once a version is release, it doesn't change, and version 0.8.2 predates this bug report

petrukhnov commented 6 years ago

Above, it says that it is "fixed with 63d72d3", which belongs to https://github.com/JetBrains/kotlin-native/pull/1876 On the page: https://github.com/JetBrains/kotlin-native/releases/tag/v0.8.2, it says "Bugfixes: ...... #1876."

Anyway, thx for information, that it is not released yet.

LouisCAD commented 6 years ago

Hum, weird… maybe I'm mistaken then

On Tue, Aug 21, 2018, 11:19 AM Konstantin Petrukhnov < notifications@github.com> wrote:

Above, it says that it is "fixed with 63d72d3 https://github.com/JetBrains/kotlin-native/commit/63d72d3bb358580ec1b51d25b7e327f3efd931b2", which belongs to #1876 https://github.com/JetBrains/kotlin-native/pull/1876 On the page: https://github.com/JetBrains/kotlin-native/releases/tag/v0.8.2, it says "Bugfixes: ...... #1876 https://github.com/JetBrains/kotlin-native/pull/1876."

Anyway, thx for information, that it is not released yet.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/JetBrains/kotlin-native/issues/1873#issuecomment-414609172, or mute the thread https://github.com/notifications/unsubscribe-auth/AGpvBZpZzoXntLVk8Xe5wEbLSabIwZ8oks5uS9CwgaJpZM4V6F4V .

petrukhnov commented 6 years ago

With 0.9 it looks fixed. No crash anymore.