我正在编写一个程序包,其中包含一个可以启动HTTP服务器的控制器和一个监视程序,以便在给定特定的HTTP请求时停止服务器。但是,当监视程序尝试关闭HTTP服务器时,由于nil指针,程序将随机崩溃。它会在3次尝试中崩溃两次。我简化了下面的代码。如果代码正常工作,它应该在第一个请求后关闭HTTP服务器。但是,它将在三次尝试中仅适当关闭一次。其他两次尝试最终将以零指针恐慌。
// 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也无济于事,添加延迟到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)
}
}
主要()中相关代码的更改仍会随机崩溃。
我不知道这里发生了什么。我在4.13.0-32通用的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
答案 0 :(得分:5)
令人恐慌,因为您在关闭服务器时发送了nil上下文。
for {
if srv.closeIdleConns() {
return lnerr
}
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
}
这是http.Server.Shutdown
的摘录。因为上下文是nil并且函数期望你发送一个非零上下文,所以它很恐慌。
您可以发送context.Background()
c.signalServer.Shutdown(context.Background())