fix: prevent SIGTERM/reload deadlock (#7562)

This commit is contained in:
Ville Vesilehto
2025-09-19 14:01:53 +03:00
committed by GitHub
parent 5532ba8484
commit 6ec327836b
6 changed files with 171 additions and 24 deletions

View File

@@ -105,6 +105,10 @@ func hook(event caddy.EventName, info any) error {
// now lets consider that plugin will not be reload, unless appear in next config file
// change status of usage will be reset in setup if the plugin appears in config file
r.setUsage(maybeUsed)
// If shutdown is in progress, avoid attempting a restart.
if shutdownRequested(r.quit) {
return
}
_, err := instance.Restart(corefile)
reloadInfo.WithLabelValues("sha512", hex.EncodeToString(sha512sum[:])).Set(1)
if err != nil {
@@ -126,3 +130,14 @@ func hook(event caddy.EventName, info any) error {
return nil
}
// shutdownRequested reports whether a shutdown has been requested via quit channel.
// helps with unit testing of the shutdown gate logic.
func shutdownRequested(quit <-chan bool) bool {
select {
case <-quit:
return true
default:
return false
}
}

View File

@@ -0,0 +1,50 @@
package reload
import (
"testing"
"github.com/coredns/caddy"
)
// fakeInput implements caddy.Input for testing parse().
type fakeInput struct {
p string
b []byte
}
func (f fakeInput) ServerType() string { return "dns" }
func (f fakeInput) Body() []byte { return f.b }
func (f fakeInput) Path() string { return f.p }
// TestParseInvalidCorefile ensures parse returns an error for invalid Corefile syntax.
func TestParseInvalidCorefile(t *testing.T) {
t.Parallel()
broken := fakeInput{p: "Corefile", b: []byte(". { errors\n")}
if _, err := parse(broken); err == nil {
t.Fatalf("expected parse error for invalid Corefile, got nil")
}
}
// TestShutdownGate ensures the shutdown gate helper recognizes when shutdown is requested.
func TestShutdownGate(t *testing.T) {
t.Parallel()
q := make(chan bool, 1)
if shutdownRequested(q) {
t.Fatalf("expected no shutdown before signal")
}
q <- true
if !shutdownRequested(q) {
t.Fatalf("expected shutdown after signal")
}
}
// TestHookIgnoresNonStartupEvent ensures hook is a no-op for non-startup events.
func TestHookIgnoresNonStartupEvent(t *testing.T) {
t.Parallel()
if err := hook(caddy.EventName("not-startup"), nil); err != nil {
t.Fatalf("expected no error for non-startup event, got %v", err)
}
}

View File

@@ -20,7 +20,7 @@ func init() { plugin.Register("reload", setup) }
// channel for QUIT is never changed in purpose.
// WARNING: this data may be unsync after an invalid attempt of reload Corefile.
var (
r = reload{dur: defaultInterval, u: unused, quit: make(chan bool)}
r = reload{dur: defaultInterval, u: unused, quit: make(chan bool, 1)}
once, shutOnce sync.Once
)