Adam Copp
12/17/2025, 10:34 AMflowOn to cause ThreadContextElement callbacks not to be called if the collector and emitter of a flow have the same dispatcher. Then we observe a mismatch between the CoroutineContext and the ThreadLocal state in that case.
I'm pretty sure this is a bug and not my bad expectations, but It's very possible I'm expecting too much.
A colleague of mine has filed a github issue and I've created a minimal repro test which demonstrates the bad behavior, but I'm not sure how we could go about mitigating it. My best guess is that we need to somehow note the whole stack of ThreadContextElements when ChannelFlow sees a COROUTINE_SUSPENDED, and then restore it in withContext? I'm very inexperienced working at this level of the coroutines implementation details but I'm willing to contribute if I can get help with figuring out an approach.