Panics: When a program stops
● Intermediate2 min
How a program ends tells the shell what happened: 0 for success, 1 for an error from main, 101 for a panic in main.
| What happened | Printed to standard error | Exit code |
|---|---|---|
| It finished | nothing | 0 |
main returned an error | error: <message> | 1 |
| A panic in the main task | panic: <message>, then at <file>:<line> | 101 |
Ctrl+C (SIGINT) | nothing | 130 |
The terminal or console closed (SIGHUP) | nothing | 129 |
SIGTERM (how Docker and systemd stop a program; on Windows, logging off or shutting down) | nothing | 143 |
These messages never go to standard output, so a program's real output stays clean when it is piped into another program.
- Ctrl+C, a closed terminal and
SIGTERMend the program the waycli.exitdoes: what it printed is flushed, and logs and traces are sent before it ends. A second Ctrl+C ends it at once. - This holds for
kavach runand for programs built withkavach build.
Finishing before a stop
A program with work it must not cut off (requests being answered, a file half written, a queue to empty) takes a hold with cli.on_stop(grace). While a hold is kept, the first stop signal doesn't end the program: hold.asked() turns true, and the program finishes what it is doing.
use std.cli
use std.time
fn main() uses Clock {
const hold = cli.on_stop(grace: 30.seconds)
while not hold.asked() {
work_a_little()
}
save_progress() // Docker's SIGTERM: this still runs
hold.done()
}- The program ends, with the signal's exit code, once every hold is let go of with
hold.done(), when the longestgracehas passed, or whenmainreturns. - A second signal ends it at once.
- A hold needs no capability.
http.servetakes one of its own, so a server finishes the requests under way before the program ends.
Without a stop signal, hold.asked() stays false:
use std.cli
fn main() {
const hold = cli.on_stop(grace: 30.seconds)
let rounds = 0
while not hold.asked() and rounds < 3 {
rounds += 1
}
print("asked: {hold.asked()}, rounds: {rounds}")
print("saving progress")
hold.done()
}The program ran and printed:
kavach run on-stop.kav
asked: false, rounds: 3saving progress