I’m trying to understand why some of my tests are ...
# kotlin-native
s
I’m trying to understand why some of my tests are failing with
InvalidMutabilityException
on native platforms because they use
lazy{}
values. My code is single threaded and does not freeze objects anywhere. Are test graphs frozen by default on native platforms? update: TIL
objects
are frozen by default!
d
lazy
iirc, freezes it's values by default.
s
that’s what I was suspecting, but this code doesn’t use freezing anywhere:
d
That's the unsafe one. The safe one it the one I'm referring to.
s
The stacktrace mentions this unsafe variant:
Copy code
kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.UnsafeLazyImpl@a9615c98
am I looking at the wrong place?
d
Ah. You didn't share stacktrace.
Are you using this lazy property in an
object
?
s
Yea sorry, here’s the stacktrace if it’s useful:
Yes, it’s an
object
!
d
Have you annotated the
object
with
@ThreadLocal
?
If you haven't, then you have your answer.
s
Whoaa TIL objects are frozen by default!
Thanks again @Dominaezzz!
👍🏼 1
m
I've been bitten by that as well. It feels weird that objects are not lazy attached to the first thread that accesses them
I found this post interesting about testing these kind of behaviours: https://jakewharton.com/litmus-testing-kotlins-many-memory-models/
s
Yea I understand why objects are frozen automatically, but I wish the reason was easily discoverable.
m
Yep, some kind of lint warning on objects without an annotation would be helpfull
s
fwiw I ended up replacing
lazy
with
atomicLazy