Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC Server Service loses transport coroutine context #226

Open
yakivy opened this issue Nov 5, 2024 · 0 comments
Open

RPC Server Service loses transport coroutine context #226

yakivy opened this issue Nov 5, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@yakivy
Copy link

yakivy commented Nov 5, 2024

Describe the bug
I have CallID Ktor plugin installed, it sets correct MDC for SLF4J, but it is lost during actual service logic (that probably means that it is called in different than transport coroutine context)

To Reproduce
Kotlin version - 2.0.10
Gradle version - 8.8
OS (Or at least KMP platform) - mac os sonoma
Kotlin RPC version - 0.2.4
Steps to reproduce the behavior:

  • create a simple ktor server
  • install CallID plugins:
    install(CallId) {
        generate { call -> "test" }
        verify { it.isNotBlank() }
    }
    install(CallLogging) {
        callIdMdc()
    }
  • register simple RPC service:
interface FileParsingService : RPC {
  suspend fun parse(): Unit
}

class FileParsingInternalService(
  override val coroutineContext: CoroutineContext,
): FileParsingService {
  override suspend fun parse(): Unit = println("MDC: " + MDC.getCopyOfContextMap())
}

registerService<FileParsingService> { FileParsingInternalService(it) }
  • call our RPC server

Actual behavior
You see MDC: null in console

Expected behavior
You see MDC: {callId=test} in console

Additional context
Link to a small slack discussion: https://kotlinlang.slack.com/archives/C072YJ3Q91V/p1730721325535899

Probably the main cause of the problem is here:
https://github.com/Kotlin/kotlinx-rpc/blob/main/krpc/krpc-server/src/jvmMain/kotlin/kotlinx/rpc/krpc/server/internal/RPCServerService.kt#L175
Actual RPC service logic is called in the RPC service coroutine context (Child Supervisor Job) instead of transport coroutine context (that has MDCContext). My naive approach would be to make the method suspended and launch service call on kotlin.coroutines.coroutineContext, but probably it will break control flow.

@yakivy yakivy added the bug Something isn't working label Nov 5, 2024
@yakivy yakivy changed the title RPC Server Service loses coroutine context RPC Server Service loses transport coroutine context Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants