Skip to content

Can't catch syscall.SIGINT after upgrade #76

Description

@myuid

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")

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions