I am trying to achieve quit gracefully when user press Ctrl-C
. I am trying the code in Make Ctrl+C cancel the context.Context.
package main
import (
"context"
"fmt"
"os"
"os/signal"
"time"
)
func main() {
ctx := context.Background()
// trap Ctrl+C and call cancel on the context
ctx, cancel := context.WithCancel(ctx)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
defer func() {
signal.Stop(c)
cancel()
fmt.Println("Cleaned up")
}()
go func() {
select {
case <-c:
fmt.Println("Got interrupt signal")
cancel()
case <-ctx.Done():
}
fmt.Println("Stopped monitoring")
}()
select {
case <-ctx.Done():
fmt.Println("notified to quit")
case <-time.NewTimer(time.Second * 2).C:
fmt.Println("done something")
}
}
It works well as expected when user press Ctrl-c
, it console out the following:
Got interrupt signal
Stopped monitoring
notified to quit
Cleaned up
However, if it quit normally, It doesn't work as expected as below:
done something
Cleaned up
I mean it should print out Stopped monitoring
, but not. In defer cleanup
function, it called cancel()
which should trigger the select in monitoring goroutine
to quit, but not.
How to solve the issue?