From d11b648137e6c0e3186f46661d145edd2b21b933 Mon Sep 17 00:00:00 2001 From: Jared Ririe Date: Tue, 11 Jun 2019 15:24:35 -0600 Subject: [PATCH] caddytls: Fix goroutine leak when restarting Caddy (#2644) Each time the Caddyfile reloads and Caddy is restarted, caddytls.NewConfig starts a goroutine for cleaning the certificate storage. This goroutine ranges over a time.Ticker channel; although Stop is called on this ticker, Stop does not close the underlying channel so the goroutine never exits. This change adds an additional channel that is listened to in the certificate cleaning goroutine so it can exit on restarts. --- caddytls/config.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/caddytls/config.go b/caddytls/config.go index 9fca894f..6ae1f85e 100644 --- a/caddytls/config.go +++ b/caddytls/config.go @@ -140,17 +140,26 @@ func NewConfig(inst *caddy.Instance) (*Config, error) { return certmagic.Default, nil }, }) + storageCleaningTicker := time.NewTicker(12 * time.Hour) + done := make(chan bool) go func() { - for range storageCleaningTicker.C { - certmagic.CleanStorage(certmagic.Default.Storage, certmagic.CleanStorageOptions{ - OCSPStaples: true, - }) + for { + select { + case <-done: + storageCleaningTicker.Stop() + return + case <-storageCleaningTicker.C: + certmagic.CleanStorage(certmagic.Default.Storage, certmagic.CleanStorageOptions{ + OCSPStaples: true, + }) + } } }() inst.OnShutdown = append(inst.OnShutdown, func() error { certCache.Stop() - storageCleaningTicker.Stop() + done <- true + close(done) return nil })