'Ktor server crashes when I press "stop" in IntelliJ IDEA

When I press "stop" in IntelliJ IDEA to shutdown my Ktor app, it crashes with errors:

Closing daemon's stdin at end of input.
HandleCancel processing Build{id=9b020945-b350-4d12-ba2d-c2e61531d515, currentDir=/Projects/blah-blah/server}
The daemon will no longer process any standard input.
Marking the daemon as canceled, address: [c267f21c-ecbb-4990-b647-de5cbe654b4a port:51722, addresses:[localhost/127.0.0.1]]
The daemon has received a build cancellation request.

> Task :ApplicationKt.main() FAILED
:ApplicationKt.main() (Thread[Execution worker for ':',5,main]) completed. Took 5.12 secs.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':ApplicationKt.main()'.
> Build cancelled while executing task ':ApplicationKt.main()'

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 5s

I understand that Gradle was executing run task (build in logs, what's the hack?) and I interrupted its work by SIGINT from IDE. However, this is not behaviour that I need. I just want to my server could correctly start when I press "run" and shutdown when I press "stop".

build.gradle:

plugins {
    id 'application'
    id 'org.jetbrains.kotlin.jvm' version '1.6.10'
    id 'org.jetbrains.kotlin.plugin.serialization' version '1.6.10'
}

def ktor_version='1.6.7'
def exposed_version = '0.37.3'

group 'com.company'
version '0.0.3'

application {
    mainClass = 'io.ktor.server.netty.EngineMain'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.jetbrains.kotlin:kotlin-stdlib'
    // Ktor
    implementation "io.ktor:ktor-server-core:$ktor_version"
    implementation "io.ktor:ktor-server-netty:$ktor_version"
    implementation "io.ktor:ktor-serialization:$ktor_version"
    implementation 'org.jetbrains.kotlinx:kotlinx-datetime:0.3.1'
    // Logging
    implementation 'ch.qos.logback:logback-classic:1.2.10'
    // Database
    implementation 'com.zaxxer:HikariCP:5.0.1'
    implementation "org.jetbrains.exposed:exposed-core:$exposed_version"
    implementation "org.jetbrains.exposed:exposed-kotlin-datetime:$exposed_version"
    runtimeOnly "org.jetbrains.exposed:exposed-jdbc:$exposed_version"
    implementation 'org.postgresql:postgresql:42.3.1'
    // Dependency injection
    implementation 'org.kodein.di:kodein-di-framework-ktor-server-jvm:7.10.0'
}

Application.kt:

fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

@Suppress("unused")
fun Application.module(@Suppress("UNUSED_PARAMETER") testing: Boolean = false) {
    installDi()
    install(ContentNegotiation) {
        json(Json {
            prettyPrint = true
        })
    }
    installErrorHandlers()
    installRoutes()
}

And Run configuration: Screenshot

Why does this error happen? How to fix it?



Sources

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

Source: Stack Overflow

Solution Source