Skip to content

Commit 6023d4e

Browse files
committed
propagate transport coroutine context
1 parent 409652b commit 6023d4e

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

krpc/krpc-server/src/commonMain/kotlin/kotlinx/rpc/krpc/server/KrpcServer.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public abstract class KrpcServer(
6060
*/
6161

6262
@InternalRpcApi
63-
public val internalScope: CoroutineScope = CoroutineScope(SupervisorJob(transport.coroutineContext.job))
63+
public val internalScope: CoroutineScope = CoroutineScope(
64+
transport.coroutineContext + SupervisorJob(transport.coroutineContext.job)
65+
)
6466

6567
private val logger = RpcInternalCommonLogger.logger(rpcInternalObjectId())
6668

krpc/krpc-test/src/commonTest/kotlin/kotlinx/rpc/krpc/test/CoroutineContextPropagationTest.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package kotlinx.rpc.krpc.test
77
import kotlinx.coroutines.CoroutineScope
88
import kotlinx.coroutines.currentCoroutineContext
99
import kotlinx.coroutines.test.runTest
10+
import kotlinx.coroutines.withContext
1011
import kotlinx.rpc.krpc.rpcClientConfig
1112
import kotlinx.rpc.krpc.rpcServerConfig
1213
import kotlinx.rpc.krpc.serialization.json.json
@@ -29,7 +30,7 @@ class CoroutineContextPropagationTest {
2930
}
3031
}
3132

32-
object CoroutineElement : CoroutineContext.Element {
33+
data class CoroutineElement(val value: String) : CoroutineContext.Element {
3334
object Key : CoroutineContext.Key<CoroutineElement>
3435

3536
override val key: CoroutineContext.Key<*> = Key
@@ -38,20 +39,22 @@ class CoroutineContextPropagationTest {
3839
@Test
3940
fun test() = runTest {
4041
var actualContext: CoroutineElement? = null
41-
val transport = LocalTransport(transportContext = CoroutineElement)
42+
val transport = LocalTransport(transportContext = CoroutineElement("transport"))
4243
val server = KrpcTestServer(rpcServerConfig, transport.server)
4344
val client = KrpcTestClient(rpcClientConfig, transport.client)
44-
server.registerService(Echo::class) {
45-
object : Echo {
46-
override suspend fun echo(message: String): String = run {
47-
actualContext = currentCoroutineContext().get(CoroutineElement.Key)
48-
"response"
45+
withContext(CoroutineElement("server")) {
46+
server.registerService(Echo::class) {
47+
object : Echo {
48+
override suspend fun echo(message: String): String = run {
49+
actualContext = currentCoroutineContext().get(CoroutineElement.Key)
50+
"response"
51+
}
4952
}
50-
51-
override val coroutineContext: CoroutineContext = it
5253
}
5354
}
54-
client.withService(Echo::class).echo("request")
55-
assertEquals(actualContext, CoroutineElement)
55+
withContext(CoroutineElement("client")) {
56+
client.withService(Echo::class).echo("request")
57+
}
58+
assertEquals(CoroutineElement("transport"), actualContext)
5659
}
5760
}

0 commit comments

Comments
 (0)