mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-02 23:28:22 +03:00
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.
This commit is contained in:
parent
1867ded14c
commit
4de9d64c0c
1 changed files with 35 additions and 13 deletions
44
caddy.go
44
caddy.go
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
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() {
|
||||||
|
defer func() {
|
||||||
|
inst.wg.Done()
|
||||||
|
stopWg.Done()
|
||||||
|
}()
|
||||||
|
errChan <- s.Serve(ln)
|
||||||
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
errChan <- s.Serve(ln)
|
defer func() {
|
||||||
defer inst.wg.Done()
|
inst.wg.Done()
|
||||||
|
stopWg.Done()
|
||||||
}()
|
}()
|
||||||
errChan <- s.ServePacket(pc)
|
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,16 +807,24 @@ 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)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
case <-stopChan:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
stopWg.Wait()
|
||||||
|
stopChan <- struct{}{}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in a new issue