<commit_before>package org.jetbrains.ktor.servlet

import com.typesafe.config.*
import org.jetbrains.ktor.application.*
import javax.servlet.annotation.*
import javax.servlet.http.*

@MultipartConfig
open class ServletApplicationHost() : HttpServlet() {
    private val loader: ApplicationLoader by lazy {
        val servletContext = servletContext
        val parameterNames = servletContext.initParameterNames.toList().filter { it.startsWith("org.jetbrains.ktor") }
        val parameters = parameterNames.toMapBy({ it.removePrefix("org.jetbrains.") }, { servletContext.getInitParameter(it) })

        val config = ConfigFactory.parseMap(parameters)
        val configPath = "ktor.config"

        val combinedConfig = if (config.hasPath(configPath)) {
            val configStream = servletContext.classLoader.getResourceAsStream(config.getString(configPath))
            val loadedKtorConfig = ConfigFactory.parseReader(configStream.bufferedReader())
            config.withFallback(loadedKtorConfig)
        } else
            config

        val applicationLog = SLF4JApplicationLog("ktor.application")
        val applicationConfig = HoconApplicationConfig(combinedConfig, servletContext.classLoader, applicationLog)
        ApplicationLoader(applicationConfig)
    }

    val application: Application get() = loader.application


    public override fun destroy() {
        loader.dispose()
    }

    protected override fun service(request: HttpServletRequest, response: HttpServletResponse) {
        response.characterEncoding = "UTF-8"
        request.characterEncoding = "UTF-8"

        try {
            val applicationRequest = ServletApplicationRequestContext(application, request, response)
            val requestResult = application.handle(applicationRequest)
            when (requestResult) {
                ApplicationRequestStatus.Handled -> {
                    applicationRequest.close()
                }
                ApplicationRequestStatus.Unhandled -> {
                    response.sendError(HttpServletResponse.SC_NOT_FOUND)
                    applicationRequest.close()
                }
                ApplicationRequestStatus.Asynchronous -> request.startAsync()
            }
        } catch (ex: Throwable) {
            application.config.log.error("ServletApplicationHost cannot service the request", ex)
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.message)
        }
    }

}<commit_msg>Fix tomcat async hang due to missing complete() call on AsyncContext<commit_after>package org.jetbrains.ktor.servlet

import com.typesafe.config.*
import org.jetbrains.ktor.application.*
import javax.servlet.annotation.*
import javax.servlet.http.*

@MultipartConfig
open class ServletApplicationHost() : HttpServlet() {
    private val loader: ApplicationLoader by lazy {
        val servletContext = servletContext
        val parameterNames = servletContext.initParameterNames.toList().filter { it.startsWith("org.jetbrains.ktor") }
        val parameters = parameterNames.toMapBy({ it.removePrefix("org.jetbrains.") }, { servletContext.getInitParameter(it) })

        val config = ConfigFactory.parseMap(parameters)
        val configPath = "ktor.config"

        val combinedConfig = if (config.hasPath(configPath)) {
            val configStream = servletContext.classLoader.getResourceAsStream(config.getString(configPath))
            val loadedKtorConfig = ConfigFactory.parseReader(configStream.bufferedReader())
            config.withFallback(loadedKtorConfig)
        } else
            config

        val applicationLog = SLF4JApplicationLog("ktor.application")
        val applicationConfig = HoconApplicationConfig(combinedConfig, servletContext.classLoader, applicationLog)
        ApplicationLoader(applicationConfig)
    }

    val application: Application get() = loader.application


    public override fun destroy() {
        loader.dispose()
    }

    protected override fun service(request: HttpServletRequest, response: HttpServletResponse) {
        response.characterEncoding = "UTF-8"
        request.characterEncoding = "UTF-8"

        try {
            val applicationRequest = ServletApplicationRequestContext(application, request, response)
            val requestResult = application.handle(applicationRequest)
            when (requestResult) {
                ApplicationRequestStatus.Handled -> {
                    applicationRequest.close()
                }
                ApplicationRequestStatus.Unhandled -> {
                    response.sendError(HttpServletResponse.SC_NOT_FOUND)
                    applicationRequest.close()
                }
                ApplicationRequestStatus.Asynchronous -> applicationRequest.continueAsync(request.startAsync())
            }
        } catch (ex: Throwable) {
            application.config.log.error("ServletApplicationHost cannot service the request", ex)
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.message)
        }
    }

}
{'fraction_non_alphanumeric': 0.08282025819265144, 'fraction_numerical': 0.0011916583912611719, 'mean_word_length': 2.879815100154083, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5045028', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1186, 'n_words': 234}