cloudflare / tableflip

Graceful process restarts in Go
BSD 3-Clause "New" or "Revised" License
2.91k stars 148 forks source link

Can't catch syscall.SIGINT after upgrade #76

Open myuid opened 2 years ago

myuid commented 2 years ago

I can catch syscall.SIGINT when I call upgrade(), but I can't catch syscall.SIGINT when upgrade() is called. What if I restart and shut down gracefully?

func main() { upg, _ := tableflip.New(tableflip.Options{}) defer upg.Stop()

go func() {
    sig := make(chan os.Signal, 1)
    //
    signal.Notify(sig, syscall.SIGHUP, os.Interrupt, syscall.SIGTERM)
    for ch := range sig {
        switch ch {
        case syscall.SIGHUP:
            err := upg.Upgrade()
            if err != nil {
                log.Fatal(err)
            }
        default:
            upg.Interrupt()
        }
    }
}()

ln, err := upg.Listen("tcp", ":8080")
if err != nil {
    log.Fatalln("Can't listen:", err)
}
defer func(ln net.Listener) {
    _ = ln.Close()
}(ln)

count := 0
router := gin.Default()
router.GET("/", func(c *gin.Context) {
    time.Sleep(5 * time.Second)
    count++
    c.String(http.StatusOK, strconv.Itoa(count))
})

server := http.Server{
    Handler: router,
}
go func() {
    if err := server.Serve(ln); err != http.ErrServerClosed {
        log.Fatal("listen: ", err)
    }
}()

// Listen must be called before Ready

if err := upg.Ready(); err != nil {
    log.Fatal(err)
}

<-upg.Exit()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
    log.Fatal("Server forced to shutdown:", err)
}
println("going to shutdown")

}

dayueba commented 1 year ago

kill -s HUP pid