douyongdao4046 2017-09-13 17:16
浏览 206
已采纳

捕获SIGINT和SIGTERM时,Go进程过早死亡

I have a fairly straight forward signal handling and cleanup process:

func signalHandler(shutdown func() error) {
    // Make signal channel and register notifiers for Interupt and Terminate
    sigchan := make(chan os.Signal, 1)
    signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM)

    // Block until we receive a signal on the channel
    <-sigchan

    // Shutdown now that we've received the signal
    if err := shutdown(); err != nil {
        msg := fmt.Sprintf("shutdown error: %s", err.Error())
        log.Fatal(msg)
    }

    log.Println("shutdown procedure complete") 

    // Make a clean exit
    os.Exit(0)
}

This hander is running a go routine from the server:

func (s *Server) Run() error {
    go signalHandler(s.Shutdown) 
    ...

    <-s.done // block until done 
    log.Println("done running!") 
    return nil 
}

func (s *Server) Shutdown() error {
    s.done <- true 
    log.Println("all done here!") 
    return nil 
}

The problem is that the process exits before the "all done here!" can be printed to the log, and sometimes "done running" prints and other times it doesn't. It seems like there is an os.Exit() call somewhere else (possibly in one of the libraries I'm using?). I do need to clean up connections and delete temporary files etc, and they're not getting cleaned up properly.

Does anyone know how I diagnose where the process is exiting?

Update: I'm also not seeing "shutdown procedure complete" with the addition of that log statement. It appears that the process is terminating before this function is over.

  • 写回答

1条回答 默认 最新

  • dto52236 2017-09-13 17:39
    关注

    The issue is that the call to Run() is the last real op in main(). As soon as Run() returns, main() returns, and when main() returns, the program exits. I generally put signal handling in main() itself, rather than in any goroutine, to ensure that main() does not return before I'm done handling post-signal shutdown procedures.

    (From the comments, reposting as an answer now that it's sorted.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VB.NET2022如何生成发布成exe文件
  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印