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?
Thanks @Zan Lynx, I worked out the below solution.
package main
import (
"context"
"fmt"
"os"
"os/signal"
"time"
)
func main() {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
terminated := monitor(ctx, cancel)
defer func() {
cancel()
fmt.Println("Cleaned up")
<-terminated // wait for the monior goroutine quit
}()
select {
case <-ctx.Done():
fmt.Println("notified to quit")
case <-time.NewTimer(time.Second * 1).C:
fmt.Println("done something")
}
}
func monitor(ctx context.Context, cancel context.CancelFunc) <-chan interface{} {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
terminated := make(chan interface{})
go func() {
defer close(terminated)
defer fmt.Println("Stopped monitoring1")
defer signal.Stop(c)
select {
case <-c:
fmt.Println("Got interrupt singnal")
cancel()
case <-ctx.Done():
}
}()
return terminated
}