ExpediaGroup/graphql-kotlin

Unmanaged CoroutineScope in GraphQLServer

Closed this issue · 2 comments

I saw that inside servers/graphql-kotlin-server/src/main/kotlin/com/expediagroup/graphql/server/execution/GraphQLServer.kt an unmanaged CoroutineScope is created. I cannot find any code that manages (aka cancels) this newly created scope.

val graphQLExecutionScope = CoroutineScope(
    coroutineContext + customCoroutineContext + SupervisorJob()
)

I am not super knowledgeable in the handling of coroutines but I think this could potentially lead to issues where the launched coroutine continues to run even if the server/application is no longer running.

I think it would be better to launch based on the existing server/application coroutine scope. This would ensure that the launched coroutine is canceled when the server/application is shutting down.

Or is this explicitly not desired?

I am thinking about this because I would like to start long-running task within a graphQLQuery handler in Ktor; without waiting for the long-running task to finish, I would like to immediately send a graphQL response.

Without graphql-kotlin-server or graphql-kotlin-ktor-server it would be

call.application.launch(Dispatchers.IO) {
  // long running task
}

This would re-use the Ktor application CoroutineScope. This scope is managed and cancelled when the Ktor server is shut down. See https://api.ktor.io/ktor-server/ktor-server-core/io.ktor.server.application/-application/index.html.

If I would do the same in a graphQL request handler, I would have a strange situation because I would start a coroutine in the managed Ktor application scope from within the unmanaged graphql-kotlin-server scope.

I think I misunderstood what is happening. Will close the ticket.