Skip to content

reload daemon then http server can't listen on other port #64

@qianxiansheng90

Description

@qianxiansheng90

env:
Linux l-qfy0.dba.dev.cn0 3.10.0-327.28.3.el7.x86_64 #1 SMP Thu Aug 18 19:05:49 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

go version go1.17 linux/amd64

test.go

import (
	"context"
	"flag"
	"fmt"
	"os/exec"
	"strconv"
	"syscall"

	"github.com/fvbock/endless"
	"github.com/gin-gonic/gin"
)

var ctx context.Context
var cancel context.CancelFunc
var reload bool

var port = flag.Int("port", 8080, "")

func main() {
	flag.Parse()
	ListenHTTP(setReload)
}

func setReload() {
	reload = true
}
func ListenHTTP(reloadFunc func()) error {
	var handler = gin.Default()
	handler.GET("/", startDaemon())
	handler.GET("/hello", hello())

	ctx, cancel = context.WithCancel(context.Background())
	defer cancel()
	s := endless.NewServer(":"+strconv.Itoa(*port), handler)
	go func() {
		select {
		case <-ctx.Done():
		}
		if s != nil {
			if err := s.Shutdown(ctx); err != nil {
				fmt.Println(err)
			}
		}
	}()
	s.SignalHooks[endless.PRE_SIGNAL][syscall.SIGHUP] = append(
		s.SignalHooks[endless.PRE_SIGNAL][syscall.SIGHUP],
		reloadFunc)
	if err := s.ListenAndServe(); err != nil {
		return err
	}
	return nil
}
func hello() gin.HandlerFunc {
	return func(c *gin.Context) {
		fmt.Println("hello ", *port)
	}
}
func startDaemon() gin.HandlerFunc {
	return func(c *gin.Context) {
		start(c)
	}
}

func start(c *gin.Context) (err error) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	cmd := exec.CommandContext(ctx, "sh", []string{"start.sh"}...)
	err = cmd.Run()
	if err != nil {
		fmt.Println(err)
		return
	}
	return
}

start.sh

nohup /home/qfy/tmp/test --port=8081 > /dev/null 2>&1 &

basedir:/root/tmp

steps:

  1. run test

image

  1. start with new shell, then send http requst,now 8081 is listening

image

  1. kill 8081 ,and reload test

image

4.then send http requst,but 8081 is not listening
image

Is that a bug need to fix ?
And how can I resolve it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions