https://kotlinlang.org logo
#coroutines
Title
# coroutines
v

vineethraj49

08/14/2020, 1:25 PM
https://pl.kotl.in/kbrTePWXD the following runs into a deadlock(?) in kotlin 1.3.72; all the default dispatcher worker threads are in a MONITOR state 🤔 can someone point me in the direction to figuring this out?
return synchronized(lock) {
val _v2 = _value
if (_v2 !== UNINITIALIZED_VALUE) {
@Suppress("UNCHECKED_CAST") (_v2 as T)
} else {
val typedValue = initializer!!() <-- gets till this invocation and then blocked
_value = typedValue
initializer = null
typedValue
}
}
s

streetsofboston

08/14/2020, 1:32 PM
Looks like the code of a
lazy
delegate….
v

vineethraj49

08/14/2020, 1:33 PM
yes, i was setting a debug point to see how far it got
s

streetsofboston

08/14/2020, 1:35 PM
What happens if you decrease the
concurrency
value to 64 (or a bit less)?
v

vineethraj49

08/14/2020, 1:35 PM
reducing concurrency works
whatever kotlin version play.kotlinlang is on, also works regardless of concurrency
so I'm assuming there's a workaround for 1.3.72 🤞
s

streetsofboston

08/14/2020, 1:36 PM
I have such a nagging feeling that all (at least 64 of them) the asyncs (the 128 of them) are executing and suspending. Then a blocking
lazy{ .. }
is executed for the
lz
variable that can’t finish, since there are no threads left to run that
test
function…
If the many
async
were to be scheduled in a bit more sequential fashion, not all 64 IO threads would be unavailable? What happens if you don’t make
lz
lazy, but just
val lz = Obj()
?
v

vineethraj49

08/14/2020, 1:42 PM
that works ¯\_(ツ)_/¯
but Obj init is a bit costly 😞
w

wasyl

08/14/2020, 1:50 PM
What if you used a lazy
Deferred
instead?
v

vineethraj49

08/14/2020, 1:51 PM
then I'll have to change a bunch of legacy code to be suspend so I can do a .await
👍 1
which I can't
w

wasyl

08/14/2020, 1:53 PM
From non-suspending context you could have a delegated
by lazy { runBlocking { …
. Just throwing ideas
s

streetsofboston

08/14/2020, 2:08 PM
Couldn’t you move
val lz = Obj()
as the first statement in the GlobalScope.launch?
Copy code
GlobalScope.launch {
    val lz = with(<http://Dispatchers.IO|Dispatchers.IO>) { Obj() } // use IO, since creation of Obj() is blocking
    val concurrency = 128
    ...
}
v

vineethraj49

08/14/2020, 2:15 PM
I figure this is happening because Dispatchers.IO is "full" (64 already running) and another withContext/runBlocking(Dispatchers.IO) cannot start up but why does the same code work on 1.4
3 Views