Hei ! I see Ktor is very relaxed on using GlobalSc...
# ktor
s
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]
a
@e5l
r
It needs to be discussed on case by case basis, but usually we use
GlobalScope
to copy one
ByteChannel
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
s
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 ?
r
It’s not one or another. Scopes contain dispatcher. Sometimes we run coroutines in global scope, but on
<http://Dispatchers.IO|Dispatchers.IO>
👍 1