简体   繁体   English

为什么 kill -15 没有优雅地杀死我的 Golang gRPC 服务?

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

I have used signal handler to handle SIGTERM , SIGINT signals.我使用信号处理程序来处理SIGTERMSIGINT信号。 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:当 grpc 服务器启动并运行时,我发出sudo kill -15 [PID]命令,我没有看到我的正常关机日志报告,而且我得到:

[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.现在,当我使用 netstat 时,它报告端口号 50051 已打开,由于端口号繁忙,我无法运行我的服务器。

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. server.Stop() function 用于停止 grpc 服务器grpcServer.GracefulStop() ,并记录一些数据。 When I issue CTRL+C everything works as expected.当我发出CTRL+C时,一切都按预期工作。 Why on sudo kill -15 I this behavior?为什么sudo kill -15我有这种行为?

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.不要使用 go 运行。 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).使用 go build 或 go install 代替,或者至少不要杀死 -15 go 进程,但二进制文件已在/tmp.

JimB:吉姆B:

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).我知道我们每天要说 100 次,但不要使用 go 运行(至少如果你不明白它在做什么)。 When you ctrl+c from the terminal, it sends signal the the entire process group, which you are not doing with kill.当您从终端按 ctrl+c 时,它会向整个进程组发送信号,而您没有使用 kill。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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