Os.interrupt
func Os.interrupt(): ContextOs.interrupt returns a context that is done, with the reason "interrupt", when the process is interrupted by Ctrl-C, SIGINT, or SIGTERM. It is the same context at every call. Once it has been called, the first interrupt no longer ends the run; a second does, as does the first after the program cancels the context itself.
Details
- The program has to watch the context and finish on its own. If it cancels the context itself, the reason becomes
"cancelled"and the next interrupt ends the run. - When an interrupt ends the run,
antprints nothing and ends by that signal, so a shell sees status 130 for Ctrl-C and SIGINT and 143 for SIGTERM. - While a goroutine waits in Cmd.run,
antholds back SIGINT: the child, or a pipeline's last stage, gets Ctrl-C too. The context is done only when that child exits because of the signal, so quittinglesswith Ctrl-C does not end the script. SIGTERM is not held back. Holding back is process-wide, so a server that waits forOs.interrupt()does not callCmd.run.
Basic examples
Wait for Ctrl-C, then finish the work and exit:
println("running, press Ctrl-C to stop")
Os.interrupt().wait()
println("stopping")running, press Ctrl-C to stop
stoppingBefore any interrupt the context is not done and has no reason. After one, its reason is "interrupt":
let stop = Os.interrupt()
println(stop.isDone())
println(stop.reason())
stop.wait()
println(stop.reason())false
null
interruptScope
Every call returns the same context, so a library and the script that uses it watch one interrupt:
let stop = Os.interrupt()
println(Os.interrupt() == stop)trueSIGTERM, which kill and service managers send, makes the context done as Ctrl-C does. Here the program sends it to itself:
let stop = Os.interrupt()
try Cmd.read(Cmd.new("kill", ["-TERM", "${Os.pid()}"]))
stop.wait()
println(stop.reason())interruptApplications
Run a loop until Ctrl-C. Passing the context to Time.sleep ends the pause at once instead of after the full ten seconds:
let stop = Os.interrupt()
var rounds = 0
while !stop.isDone() {
rounds += 1
Time.sleep(10s, ctx: stop)
}
println("stopped after ${rounds} round")stopped after 1 roundServe HTTP until Ctrl-C. Http.serve returns Ok once the context is done and the requests in flight have finished:
let l = try Net.listen("127.0.0.1:0")
let served = Http.serve(l, { Http.Reply{ status: 200, headers: Map{}, body: "hi" } }, ctx: Os.interrupt())
println(served)Ok{ value: void }Give the shutdown a limit of its own. A child of the interrupt context is done at Ctrl-C, with the reason "interrupt", or when its timeout passes, with "timeout":
let job = Context.new(timeout: 50ms, parent: Os.interrupt())
job.wait()
println(job.reason())timeoutDrain a queue of jobs, and stop taking new ones at Ctrl-C. Channel.values ends when the context is done:
let stop = Os.interrupt()
let jobs: Channel[string] = Channel.new(capacity: 2)
jobs.send("resize")
jobs.send("upload")
for job in jobs.values(ctx: stop) {
println("done ${job}")
}
println(stop.reason())done resize
done upload
interruptProperties and relations
The context is the same at every call, and an interrupt gives it the reason "interrupt":
let stop = Os.interrupt()
assert(stop == Os.interrupt())
assert(stop.reason() == null)
try Cmd.read(Cmd.new("kill", ["-INT", "${Os.pid()}"]))
stop.wait()
assert(stop.reason() == "interrupt")
assert(Os.interrupt().isDone())Possible issues
A program that never calls Os.interrupt ends at the first Ctrl-C. This one prints start and exits with status 130, and nothing after the interrupt runs:
println("start")
try Cmd.read(Cmd.new("kill", ["-INT", "${Os.pid()}"]))
Time.sleep(1s)
println("not reached")A program that calls Os.interrupt and never watches the context ignores the first Ctrl-C. Only the second one ends it:
Os.interrupt()
try Cmd.read(Cmd.new("kill", ["-INT", "${Os.pid()}"]))
Time.sleep(100ms)
println("still running")still runningThe second interrupt ends the run even while the program is shutting down. This one prints cleaning up and exits with status 130:
let stop = Os.interrupt()
try Cmd.read(Cmd.new("kill", ["-INT", "${Os.pid()}"]))
stop.wait()
println("cleaning up")
try Cmd.read(Cmd.new("kill", ["-INT", "${Os.pid()}"]))
Time.sleep(1s)
println("not reached")Cancelling the context yourself gives it the reason "cancelled", and the next interrupt ends the run at once. This one prints cancelled and exits with status 130:
let stop = Os.interrupt()
stop.cancel()
println(stop.reason())
try Cmd.read(Cmd.new("kill", ["-INT", "${Os.pid()}"]))
Time.sleep(1s)
println("not reached")While Cmd.run waits, Ctrl-C goes to the child, and the context becomes done only if the child exits because of it. A pager quit with Ctrl-C leaves the context running, so this prints false. The holding back is process-wide, so a server that waits for Os.interrupt() does not call Cmd.run:
let stop = Os.interrupt()
try Cmd.run(Cmd.new("less", ["notes.txt"]))
println(stop.isDone())See also
Context.wait · Context.reason · Context.new · Os.exit · Cmd.run · Http.serve
