'Ktor - kotlinx.coroutines.channels.ClosedReceiveChannelException: Channel was closed

I am receiving back this kotlinx.coroutines.channels.ClosedReceiveChannelException upon about 50% of my api calls to a post url through Ktor HttpClient.

Our code looks like

Module.kt

bind<ServiceClient>() with singleton {
    val client = HttpClient(CIO) {
        install(JsonFeature) {
            serializer = KotlinxSerializer()
        }
    }
    ServiceClient( client, instance() )
}

and our implementation of the call is

suspend fun post(request: RequestObject): List<ResponseObjects> =
    client.post(endpoint) {
        contentType(ContentType.Application.Json)
        body = request

    }

Sometimes I am able to receive back the expected results and other times I get the client closed exception. What am I doing wrong here?

I am on Ktor version 1.6.4



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source