Repository: ktorio/ktor
File: ktor-server/ktor-server-netty/jvm/src/io/ktor/server/netty/NettyApplicationResponse.kt (Kotlin)

Code before:
        responseReady.setSuccess()
        responseMessageSent = true

        if (isInfoOrNoContentStatus()) {
            if (call is NettyApplicationCall) {
                (call as NettyApplicationCall).responseWriteJob.join()
            }
        }
    }

    internal fun isInfoOrNoContentStatus(): Boolean {
        val status = status()
        return (status != null) && (status == HttpStatusCode.NoContent || (status.value >= 100 && status.value < 200))
    }

    override suspend fun responseChannel(): ByteWriteChannel {
        val channel = ByteChannel()
        val chunked = headers[HttpHeaders.TransferEncoding] == "chunked"
        sendResponse(chunked, content = channel)

        if (isInfoOrNoContentStatus()) {
            if (call is NettyApplicationCall) {
                (call as NettyApplicationCall).responseWriteJob.join()
            }
        }

        return channel
    }

    override suspend fun respondNoContent(content: OutgoingContent.NoContent) {
        respondFromBytes(EmptyByteArray)
    }

    protected abstract fun responseMessage(chunked: Boolean, last: Boolean): Any

    /**
     * Returns http response object with [data] content
     */
    protected open fun responseMessage(chunked: Boolean, data: ByteArray): Any = responseMessage(chunked, true)

    /**
     * Returns http trailer message
     */
    internal open fun prepareTrailerMessage(): Any? {
        return null
    }

    internal fun sendResponse(chunked: Boolean = true, content: ByteReadChannel) {
        if (responseMessageSent) return

        responseChannel = content

Review comment (refactor):
Could you extract a function for this block and add KDoc to explain why this is necessary?  This is pretty non-local control so it's hard to understand the intent at face value.

Code after:
        responseReady.setSuccess()
        responseMessageSent = true

        awaitProcessingResponseIfInfoOrNoContent()
    }

    override suspend fun responseChannel(): ByteWriteChannel {
        val channel = ByteChannel()
        val chunked = headers[HttpHeaders.TransferEncoding] == "chunked"
        sendResponse(chunked, content = channel)

        awaitProcessingResponseIfInfoOrNoContent()

        return channel
    }

    /**
     * Await the [NettyApplicationCall.responseWriteJob] to complete if the response status is informational or 204.
     * Netty discards certain headers of such responses, so we have to wait for Netty to finish that,
     * in order to avoid the race condition.
     */
    private suspend fun awaitProcessingResponseIfInfoOrNoContent() {
        val status = status()

        if (status != null) {
            val infoOrNoContent = status == HttpStatusCode.NoContent || (status.value >= 100 && status.value < 200)

            if (infoOrNoContent && call is NettyApplicationCall) {
                (call as NettyApplicationCall).responseWriteJob.join()
            }
        }
    }

    override suspend fun respondNoContent(content: OutgoingContent.NoContent) {
        respondFromBytes(EmptyByteArray)
    }

    protected abstract fun responseMessage(chunked: Boolean, last: Boolean): Any

    /**
     * Returns http response object with [data] content
     */
    protected open fun responseMessage(chunked: Boolean, data: ByteArray): Any = responseMessage(chunked, true)

    /**
     * Returns http trailer message
     */
    internal open fun prepareTrailerMessage(): Any? {
        return null
    }