为什么kill -15不能正常终止我的Golang gRPC服务? [英] Why kill -15 does not gracefully kill my Golang gRPC service?
问题描述
我已经使用信号处理程序来处理 SIGTERM
和 SIGINT
信号.当grpc服务器启动并运行时,我发出 sudo kill -15 [PID]
命令,但看不到正常的关机日志报告,并且得到:
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
现在,当我使用netstat时,它报告端口号50051已打开,并且由于端口号繁忙而无法运行服务器.
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.
我做了什么:
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()
函数用于停止grpc服务器 grpcServer.GracefulStop()
,并记录一些数据.当我发出 CTRL + C
时,一切正常.为什么在 sudo kill -15
上出现此行为?
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.
彼得:
不要使用go run.这种方式的信号转发可能不可靠.可以使用go build或go install代替,或者至少不要杀死-15 go进程,而要杀死已构建的二进制文件(可能在/tmp中).
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:
我知道我们每天要说100次,但不要使用go run(至少如果您不了解它在做什么).当您从终端上按ctrl + c时,它将向整个进程组发送信号,而您并不需要执行kill.
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.
这篇关于为什么kill -15不能正常终止我的Golang gRPC服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!