简体   繁体   中英

Why kill -15 does not gracefully kill my Golang gRPC service?

I have used signal handler to handle SIGTERM , SIGINT signals. When grpc server is up and running I issue sudo kill -15 [PID] command and I don't see my graceful shutdown log reports and also I get:

[1]    41983 terminated  go run mypkg/main.go

Now when I use netstat it reports that port number 50051 is open and I cannot run my server as the port number is busy.

What I have done:

func main() {
    flag.Parse()
    fmt.Printf("Starting up server on 0.0.0.0:%v...\n", *port)
    server := NewServer(fmt.Sprintf("0.0.0.0:%d", *port))
    wg := sync.WaitGroup{}
    wg.Add(1)
    go func() {
        server.Run()
        wg.Done()
    }()
    // Signal handling and graceful shutdown of gRPC server
    signalChan := make(chan os.Signal, 1)
    signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
    <-signalChan
    server.Stop()
    wg.Wait()
}

server.Stop() function is used to stop the grpc server, grpcServer.GracefulStop() , and log some data. When I issue CTRL+C everything works as expected. Why on sudo kill -15 I this behavior?

As future readers might also have my problem I tried to answer my own question based on user comments.

Peter:

Don't use go run. Signal forwarding may be unreliable this way. Use go build or go install instead, or at the very least don't kill -15 the go process, but the binary that has been built (probably something in /tmp).

JimB:

I know we say this 100 times a day, but do not use go run (at least if you don't understand what it's doing). When you ctrl+c from the terminal, it sends signal the the entire process group, which you are not doing with kill.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM