mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -04:00
fix: prevent SIGTERM/reload deadlock (#7562)
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
50
plugin/reload/reload_test.go
Normal file
50
plugin/reload/reload_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user