From e56971b253e386051257c2bc594acb432e12d5bf Mon Sep 17 00:00:00 2001 From: wenxuan70 Date: Thu, 20 Nov 2025 09:17:57 +0800 Subject: [PATCH] fix: fix reload causing secondary plugin goroutine to leak. (#7694) Signed-off-by: wenxuan70 --- plugin/file/secondary.go | 8 +++++++- plugin/secondary/setup.go | 14 +++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/plugin/file/secondary.go b/plugin/file/secondary.go index 9898c697f..b95248e8c 100644 --- a/plugin/file/secondary.go +++ b/plugin/file/secondary.go @@ -107,7 +107,7 @@ func less(a, b uint32) bool { // and uses the SOA parameters. Every refresh it will check for a new SOA number. If that fails (for all // server) it will retry every retry interval. If the zone failed to transfer before the expire, the zone // will be marked expired. -func (z *Zone) Update() error { +func (z *Zone) Update(updateShutdown chan bool) error { // If we don't have a SOA, we don't have a zone, wait for it to appear. for z.SOA == nil { time.Sleep(1 * time.Second) @@ -183,6 +183,12 @@ Restart: retryTicker.Stop() expireTicker.Stop() goto Restart + + case <-updateShutdown: + refreshTicker.Stop() + retryTicker.Stop() + expireTicker.Stop() + return nil } } } diff --git a/plugin/secondary/setup.go b/plugin/secondary/setup.go index 05cb16abd..112270103 100644 --- a/plugin/secondary/setup.go +++ b/plugin/secondary/setup.go @@ -27,6 +27,9 @@ func setup(c *caddy.Controller) error { n := zones.Names[i] z := zones.Z[n] if len(z.TransferFrom) > 0 { + // In order to support secondary plugin reloading. + updateShutdown := make(chan bool) + c.OnStartup(func() error { z.StartupOnce.Do(func() { go func() { @@ -43,12 +46,21 @@ func setup(c *caddy.Controller) error { if dur > max { dur = max } + select { + case <-updateShutdown: + return + default: + } } - z.Update() + z.Update(updateShutdown) }() }) return nil }) + c.OnShutdown(func() error { + updateShutdown <- true + return nil + }) } }