From 6f019285126d250ed5875594c37d1b3277251c8e Mon Sep 17 00:00:00 2001 From: Damir Vandic Date: Mon, 24 Jun 2019 00:24:13 +0200 Subject: [PATCH] Fix graceful shutdown (#2618) Currently, the instance waitgroup is decremented twice in `startServers()`: once when `Serve()` is finished and once when `ServePacket()` is finished. However, with a graceful shutdown, `Serve()` returns before the server has actually finished shutting down all active connections. This patch increases the wait group by one when the server is shut down so that the program only exits when all the server instances have finished serving their connections. --- caddy.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/caddy.go b/caddy.go index 8e12feaf6..9f348087c 100644 --- a/caddy.go +++ b/caddy.go @@ -888,6 +888,10 @@ func Stop() error { } inst := instances[0] instancesMu.Unlock() + // Increase the instance waitgroup so that the last wait() call in + // caddymain/run.go blocks until this server instance has shut down + inst.wg.Add(1) + defer inst.wg.Done() if err := inst.Stop(); err != nil { log.Printf("[ERROR] Stopping %s: %v", inst.serverType, err) }