Hei ! I see Ktor is very relaxed on using GlobalSc...
# ktor
Hei ! I see Ktor is very relaxed on using GlobalScope in the framework. However, GlobalScope is marked as delicate api, and I have been bitten by it by abusing it unintentionally. Biggest disadvantage of using GlobalScope is, that it is not bound to any scope, thus it cannot be cancelled, in regards to structured concurrency (correct me if I am wrong). This can easily lead to memory leaks and unclosed operations. Have the team thought about moving away from GlobalScope ?
cc: @Aleksei Tirman [JB]
It needs to be discussed on case by case basis, but usually we use
to copy one
to another. In such situations it is safe to use it, because channels themselves are bound to some local scopes, and cancellation or failures of these scopes will be reflected in channels and whole operation will be cancelled.
👍 2
Aha. Nice. I have browsed through the code and mostly have seen GlobalScope being used with byte channel operations as you say. Why can't we use Dispatchers.IO instead for example ?
It’s not one or another. Scopes contain dispatcher. Sometimes we run coroutines in global scope, but on
👍 1