https://kotlinlang.org logo
#ktor
Title
# ktor
m

Mark Malik

06/07/2022, 3:57 PM
Hi Guys, I have a plugin written to expose a HealthCheck endpoint. It all works, and responds correctly, but I noticed I also get the following error:
Copy code
java.lang.UnsupportedOperationException: Headers can no longer be set because response was already completed
	at io.ktor.server.netty.http1.NettyHttp1ApplicationResponse$headers$1.engineAppendHeader(NettyHttp1ApplicationResponse.kt:42)
	at io.ktor.server.response.ResponseHeaders.append(ResponseHeaders.kt:57)
	at io.ktor.server.response.ResponseHeaders.append$default(ResponseHeaders.kt:48)
	at io.ktor.server.response.ApplicationResponsePropertiesKt.header(ApplicationResponseProperties.kt:14)
	at io.ktor.server.plugins.cors.CORSKt.corsVary(CORS.kt:226)
This is the plugin code:
Copy code
val HealthChecks: ApplicationPlugin<HealthConfig> = createApplicationPlugin("HealthChecks", ::HealthConfig) {
    on(CallSetup) { call ->
        if (call.request.path() != "/monitoring/health-check" && call.request.httpMethod != HttpMethod.Get) return@on

        var healthCheckStatus: Map<String, Boolean>
        runBlocking {
            healthCheckStatus = pluginConfig.performChecks()
        }

        val statusCode = when (healthCheckStatus.all { it.value }) {
            true -> HttpStatusCode.OK
            false -> HttpStatusCode.InternalServerError
        }
        call.respondText(Json.encodeToString(healthCheckStatus), ContentType.Application.Json, statusCode)
    }
}
I thought that I could use the plugin to expose request handlers not requiring to create a routing. Is there something invalid about this setup ? FYI. I have CORS enabled with anyHosts() option. The error is thrown in
Copy code
private fun ApplicationCall.corsVary() {
        val vary = response.headers[HttpHeaders.Vary]
        if (vary == null) {
            response.header(HttpHeaders.Vary, HttpHeaders.Origin)
        } else {
            response.header(HttpHeaders.Vary, vary + ", " + HttpHeaders.Origin)
        }
    }
r

Rustam Siniukov

06/07/2022, 4:00 PM
This is a bug on our side. Please create ticket.
83 Views