Hi guys,A question about `coroutines` I am develo...
# coroutines
z
Hi guys,A question about
coroutines
I am developing tracing for
kotlin-coroutines
and I had a test case to assert the collecting spans
Copy code
val coroutineCount = (System.getenv("COROUTINE_COUNT") ?: "200").toInt()
val executor = Executors.newFixedThreadPool(5).asCoroutineDispatcher()

runBlocking(executor) {
  repeat(coroutineCount) {
    launch {
      runBlocking {
       withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
         sampleAndStart(INTERMEDIATE, "io")
         delay(Random.nextLong(5))
         endAndCommit(INTERMEDIATE)
        }
      }
    }
  }
}
In kotlin-coroutine 1.5 the span's count is right. but I update to 1.6 it will miss some spans. do you have any ideas on this issue ❤️
u
The issue is probably on the second runBlocking. Docs say: This function should not be used from a coroutine Do you really need to block or would
coroutineScope { }
do what you intend?
z
Thanks so much❤️ @uli, yes as you said maybe we need refine our test case first , but an another question about that why it is right in 1.5 this make me confuse.
u
It works by coincident.
z
Copy code
val coroutineCount = (System.getenv("COROUTINE_COUNT") ?: "200").toInt()
val executor = Executors.newFixedThreadPool(5).asCoroutineDispatcher()

runBlocking(executor) {
  repeat(coroutineCount) {
    launch {
       withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
         sampleAndStart(INTERMEDIATE, "io")
         delay(Random.nextLong(5))
         endAndCommit(INTERMEDIATE)
        }
    }
  }
}
I remove inner
runBlocking
but found the count is not right neither. Do I miss something?
In this code
Copy code
override fun dispatch(context: CoroutineContext, block: Runnable) {
    dispatchInternal(block) {
        dispatcher.dispatch(this, this)
    }
}
and
Copy code
private inline fun dispatchInternal(block: Runnable, dispatch: () -> Unit) {
    // Add task to queue so running workers will be able to see that
    if (addAndTryDispatching(block)) return
    /*
     * Protect against the race when the number of workers is enough,
     * but one (because of synchronized serialization) attempts to complete,
     * and we just observed the number of running workers smaller than the actual
     * number (hit right between `--runningWorkers` and `++runningWorkers` in `run()`)
     */
    if (!tryAllocateWorker()) return
    dispatch()
}
I am not sure why they call each inner the function?
u
What exactly is sampleAndStart and endAndCommit?
z
In sampleAndStart and endAndCommit fucntion I just do some instruments for example add span type and span name
"INTERMEDIATE"
"io"
So that in tracer we can collect the tracing.
Untitled
Hi @uli I we really need coroutines team support. I instrumented the coroutinescheduler runsafely method, so when the continuation is dispatched and run, I print out some messages. From the trace log , you can see that the end operation is executed in thread 15, while the code I instrumented into the runsafely method is executed after that. It's not a normal case, but if it's running in multithread env, some coroutines may have the problem, while most of them are good.