Avoid deadlock (fixes #941)

This commit is contained in:
Matthew Holt 2016-07-19 12:05:40 -06:00
parent b35d19d78e
commit 1240690973
No known key found for this signature in database
GPG key ID: 0D97CC73664F4D03

View file

@ -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
}