0

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?

Alireza
  • 6,497
  • 13
  • 59
  • 132
  • 2
    Is `NewServer` your own code or a 3rd party package? The above should work. Using this [simplified version](https://play.golang.org/p/P1vw0-NJkAO) works on Linux with a `kill -15`. If `NewServer` is not your code - perhaps it has its own signal handler? – colm.anseo Mar 30 '21 at 12:38
  • 6
    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). – Peter Mar 30 '21 at 13:19
  • 3
    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`. – JimB Mar 30 '21 at 13:32

1 Answers1

-1

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.

Alireza
  • 6,497
  • 13
  • 59
  • 132