Cancel main coroutine on SIGINT

Let’s assume we have the following main function

suspend fun main() = supervisorScope {
   
     while (isActive) {
          delay(1000)
          println("Running...")
     }
}

How can I cancel the coroutine when the jvm receives a SIGINT signal?

My first approach was this

suspend fun main() = supervisorScope {
     Signal.handle(Signal("INT")) {
         this.cancel()
     }

     while (isActive) {
          delay(1000)
          println("Running...")
     }        
}

which correctly cancels the main coroutine and all their children. But unfortunately the jvm terminates with a kotlinx.coroutines.JobCancellationException exception and exit code 1 instead of exit code 0. So I tried this

suspend fun main() = try {
    supervisorScope {
        Signal.handle(Signal("INT")) {
            this.cancel()
        }
        while (isActive) {
            delay(1000)
            println("Running...")
        }    
    }
} catch (e: CancellationException) {
    //ignore exception
}

but the ignored exception is ugly.

Do you have another idea?

I’ve found a solution

suspend fun main() = supervisorScope {
    Signal.handle(Signal("INT")) {
        this.coroutineContext.cancelChildren()
    }

    task()
    task()
}

fun CoroutineScope.task() {
    launch(Default) {
        println("Starting...")
        println("Started.")
        try {
            repeat(10) {
                println("Do something...")
                delay(1000)
                println("Something done.")
            }
        } finally {
            println("Terminating...")
            println("Terminated.")
        }
    }
}

Instead of canceling the main coroutine canceling its children do the trick.