Is it okay to share the same coroutine context across different coroutines? I tested it and it seems to be working fine.
val context = Dispatchers.IO + myThreadLocal.asContextElement() + myThreadContextElement
val deferredResultOne = async(context) { /* some work */ }
val deferredResultTwo = async(context) { /* some work */ }
val resultOne = deferredResultOne.await()
val resultTwo = deferredResultTwo.await()
Yes, it's generally ok.
In fact all coroutine inherit some context from their parent scope, but they create their own child context anyway. If you want more details about this, check this article about how contexts are passed). Here is a glimpse:
Then it's a matter of whether the semantics of your custom context elements is ok with that too :)