HTTP 服务器随机关闭崩溃

标签 http go shutdown panic

我正在编写一个包,其中包含一个可以启动 HTTP 服务器的 Controller 和一个在给出特定 HTTP 请求时停止服务器的看门狗。但是,当看门狗试图关闭 HTTP 服务器时,程序将随机崩溃,因为指针为 nil。它会在 3 次尝试中崩溃大约两次。我简化了下面的代码。如果代码正常工作,它应该在第一次请求后关闭 HTTP 服务器。但是,它只会在三次尝试中正确关闭一次。其他两次尝试将以 nil pointer panic 告终。

// Controller is the controller of signal package.
// It controls the signal sub http server and make responses
// when a specific signal is given.
// It has two concurrent threads, one being the sub http server goroutine,
// the other being the WatchDog thread for rapid responses and timeout implementation.
type Controller struct {
    signal       chan int
    signalServer http.Server // The sub http server used.
}

// Start starts signal server and watchdog goroutine.
func (c *Controller) Start() {
    go c.signalServer.ListenAndServe()
    c.watchDog()
}

// Stop stops only the signal server.
// Watchdog need not and cannot stop as it can only be stopped from inside.
// Anyway, watchdog invokes Stop().
func (c *Controller) Stop() {
    log.Println("Stopping Signal server.")
    c.signalServer.Shutdown(nil)
}

// cSignalHandler gets a http handler that can access signal controller.
func (c *Controller) cSignalHandler() func(w http.ResponseWriter, r *http.Request) {
    // This is the actual handler.
    // This implementation can perform tasks without blocking this thread.
    // This handler will send 1 to channel for watchdog to handle.
    return func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Stopped"))
        c.signal <- 1
    }
}

// watchDog monitors signal through signal controller's signal channel.
// It performs responses to the signal instead of http server.
// It will shutdown http server when receives value from channel.
func (c *Controller) watchDog() {

    <-c.signal // Signal received.
    c.Stop() // Stop signal sub http server when watchDog exits.

    return
}

func main() {
    con := new(Controller)

    con.signal = make(chan int, 1)
    mux := http.NewServeMux()
    mux.HandleFunc("/signal/", con.cSignalHandler())
    con.signalServer = http.Server{
        Addr:    ":" + strconv.Itoa(8888),
        Handler: mux,
    }
    con.Start()
}

此外,将 signalServer 字段切换为 *http.Server 也无济于事,将 defer 添加到 c.Stop() 也没有效果。即使将 signalServer 字段切换为 *http.Server 并检查其 nil 也无济于事。也就是说,

type Controller struct {
    signal       chan int
    signalServer *http.Server // The sub http server used.
}

func (c *Controller) Stop() {
    log.Println("Stopping Signal server.")
    if c.signalServer != nil {
        c.signalServer.Shutdown(nil)
    }
}

并且修改main()中的相关代码仍然会随机崩溃。

我不知道这里发生了什么。我在 4.13.0-32-generic GNU/Linux 机器上使用 golang-1.9。

输出堆栈跟踪如下:

2018/02/14 13:37:50 Stopping Signal server.
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x5eab91]

goroutine 1 [running]:
net/http.(*Server).Shutdown(0xc420084ea8, 0x0, 0x0, 0x0, 0x0)
        /usr/lib/go-1.9/src/net/http/server.go:2506 +0x1b1
main.(*Controller).Stop(0xc420084ea0)
        /home/bhat/Dev/Projects/Go/signal/controller.go:31 +0x91
main.(*Controller).watchDog(0xc420084ea0)
        /home/bhat/Dev/Projects/Go/signal/controller.go:59 +0x45
main.(*Controller).Start(0xc420084ea0)
        /home/bhat/Dev/Projects/Go/signal/controller.go:23 +0x53
main.main()
        /home/bhat/Dev/Projects/Go/signal/main.go:18 +0x1a9
exit status 2

最佳答案

这很 panic ,因为您在关闭服务器时发送了一个 nil 上下文。

for {
    if srv.closeIdleConns() {
        return lnerr
    }
    select {
    case <-ctx.Done():
        return ctx.Err()
    case <-ticker.C:
    }
}

这是来自 http.Server.Shutdown 的片段。由于 context 为 nil 并且函数希望您发送非 nil 上下文,因此它会引起 panic 。 您可以通过发送 context.Background()

来修复它
c.signalServer.Shutdown(context.Background())

关于HTTP 服务器随机关闭崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48780295/

相关文章:

c++ - 如何防止系统在没有窗口的情况下关闭?

c - libcurl:连接保持事件如何工作?

http - 此响应是否正确编码?

c# - Azure ASP Web API 错误 405 不允许方法

docker - 如何在 Docker 容器中保留 go 1.11 模块?

python - 如何在 httplib2 请求太长时关闭它

c# - 如何以编程方式使系统退出待机状态?

java - 休息服务调用未触发处理程序

unit-testing - 如何在 Golang 中实现 stub ? stub 和模拟之间有什么区别?

go - 如何在测试中期待JSON