Skip to content

Can't catch syscall.SIGINT after upgrade #76

@myuid

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

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions