From 1240690973140938e7d576b2672c8c4dcb0efee2 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Tue, 19 Jul 2016 12:05:40 -0600 Subject: [PATCH] Avoid deadlock (fixes #941) --- caddy.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/caddy.go b/caddy.go index 2869ed42..401c6fc6 100644 --- a/caddy.go +++ b/caddy.go @@ -696,14 +696,19 @@ func loadServerBlocks(serverType, filename string, input io.Reader) ([]caddyfile // instances after stopping is completed. Do not re-use any // references to old instances after calling Stop. func Stop() error { - instancesMu.Lock() - for _, inst := range instances { + // This awkward for loop is to avoid a deadlock since + // inst.Stop() also acquires the instancesMu lock. + for { + instancesMu.Lock() + if len(instances) == 0 { + break + } + inst := instances[0] + instancesMu.Unlock() if err := inst.Stop(); err != nil { log.Printf("[ERROR] Stopping %s: %v", inst.serverType, err) } } - instances = []*Instance{} - instancesMu.Unlock() return nil }