Commit 4de9d64c authored by Yue Ko's avatar Yue Ko Committed by Matt Holt

core: Terminate goroutine used for logging errors (#2398)

Terminate the goroutine used for logging errors by using a
WaitGroup (stopWg) to track termination of servers.
Fixes #2358.
parent 1867ded1
...@@ -686,6 +686,11 @@ func executeDirectives(inst *Instance, filename string, ...@@ -686,6 +686,11 @@ func executeDirectives(inst *Instance, filename string,
func startServers(serverList []Server, inst *Instance, restartFds map[string]restartTriple) error { func startServers(serverList []Server, inst *Instance, restartFds map[string]restartTriple) error {
errChan := make(chan error, len(serverList)) errChan := make(chan error, len(serverList))
// used for signaling to error logging goroutine to terminate
stopChan := make(chan struct{})
// used to track termination of servers
stopWg := &sync.WaitGroup{}
for _, s := range serverList { for _, s := range serverList {
var ( var (
ln net.Listener ln net.Listener
...@@ -777,14 +782,23 @@ func startServers(serverList []Server, inst *Instance, restartFds map[string]res ...@@ -777,14 +782,23 @@ func startServers(serverList []Server, inst *Instance, restartFds map[string]res
} }
inst.wg.Add(2) inst.wg.Add(2)
go func(s Server, ln net.Listener, pc net.PacketConn, inst *Instance) { stopWg.Add(2)
defer inst.wg.Done() func(s Server, ln net.Listener, pc net.PacketConn, inst *Instance) {
go func() { go func() {
defer func() {
inst.wg.Done()
stopWg.Done()
}()
errChan <- s.Serve(ln) errChan <- s.Serve(ln)
defer inst.wg.Done()
}() }()
errChan <- s.ServePacket(pc)
go func() {
defer func() {
inst.wg.Done()
stopWg.Done()
}()
errChan <- s.ServePacket(pc)
}()
}(s, ln, pc, inst) }(s, ln, pc, inst)
inst.servers = append(inst.servers, ServerListener{server: s, listener: ln, packet: pc}) inst.servers = append(inst.servers, ServerListener{server: s, listener: ln, packet: pc})
...@@ -793,18 +807,26 @@ func startServers(serverList []Server, inst *Instance, restartFds map[string]res ...@@ -793,18 +807,26 @@ func startServers(serverList []Server, inst *Instance, restartFds map[string]res
// Log errors that may be returned from Serve() calls, // Log errors that may be returned from Serve() calls,
// these errors should only be occurring in the server loop. // these errors should only be occurring in the server loop.
go func() { go func() {
for err := range errChan { for {
if err == nil { select {
continue case err := <-errChan:
} if err != nil {
if strings.Contains(err.Error(), "use of closed network connection") { if !strings.Contains(err.Error(), "use of closed network connection") {
// this error is normal when closing the listener; see https://github.com/golang/go/issues/4373 // this error is normal when closing the listener; see https://github.com/golang/go/issues/4373
continue log.Println(err)
}
}
case <-stopChan:
return
} }
log.Println(err)
} }
}() }()
go func() {
stopWg.Wait()
stopChan <- struct{}{}
}()
return nil return nil
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment