@@ -10,9 +10,12 @@ import com.intellij.openapi.util.Disposer
10
10
import kotlinx.coroutines.CoroutineName
11
11
import kotlinx.coroutines.CoroutineScope
12
12
import kotlinx.coroutines.SupervisorJob
13
+ import kotlinx.coroutines.ThreadContextElement
13
14
import kotlinx.coroutines.cancel
14
15
import software.aws.toolkits.jetbrains.core.coroutines.getCoroutineBgContext
16
+ import software.aws.toolkits.jetbrains.services.telemetry.PluginResolver
15
17
import java.util.concurrent.CancellationException
18
+ import kotlin.coroutines.CoroutineContext
16
19
17
20
class PluginCoroutineScopeTracker : Disposable {
18
21
@PublishedApi
@@ -27,11 +30,30 @@ class PluginCoroutineScopeTracker : Disposable {
27
30
}
28
31
29
32
private class BackgroundThreadPoolScope (coroutineName : String , disposable : Disposable ) : CoroutineScope {
30
- override val coroutineContext = SupervisorJob () + CoroutineName (coroutineName) + getCoroutineBgContext()
33
+ override val coroutineContext = SupervisorJob () +
34
+ CoroutineName (coroutineName) +
35
+ getCoroutineBgContext() +
36
+ PluginResolverThreadContextElement (PluginResolver .fromCurrentThread())
31
37
32
38
init {
33
39
Disposer .register(disposable) {
34
40
coroutineContext.cancel(CancellationException (" Parent disposable was disposed" ))
35
41
}
36
42
}
37
43
}
44
+
45
+ private class PluginResolverThreadContextElement (val pluginResolver : PluginResolver ) : ThreadContextElement<PluginResolver> {
46
+ companion object Key : CoroutineContext.Key<PluginResolverThreadContextElement>
47
+
48
+ override val key = Key
49
+
50
+ override fun updateThreadContext (context : CoroutineContext ): PluginResolver {
51
+ val oldPluginResolver = PluginResolver .fromCurrentThread()
52
+ PluginResolver .setThreadLocal(pluginResolver)
53
+ return oldPluginResolver
54
+ }
55
+
56
+ override fun restoreThreadContext (context : CoroutineContext , oldState : PluginResolver ) {
57
+ PluginResolver .setThreadLocal(oldState)
58
+ }
59
+ }
0 commit comments