diff --git a/internal/cmd/server.go b/internal/cmd/server.go index 41067438cf..50a767d6b1 100644 --- a/internal/cmd/server.go +++ b/internal/cmd/server.go @@ -102,14 +102,17 @@ func server(ctx context.Context, stdout, stderr io.Writer, cfgPath string, hook // Exit if the config loader fails to start the runners. // Continuing with failed runners would cause EG to function incorrectly. case err := <-l.Errors(): - cfg.Logger.Error(err, "failed to start runners") + // Read the logger through the loader so the read is guarded by the + // same mutex the reload path holds while replacing it. Reading + // cfg.Logger directly here races with that swap. + l.Logger().Error(err, "failed to start runners") // Wait for runners to finish before shutting down. // This is to make sure no orphaned runner process is left running in standalone mode. l.Wait() return err // Wait for the context to be done, which usually happens the process receives a SIGTERM or SIGINT. case <-ctx.Done(): - cfg.Logger.Info("shutting down") + l.Logger().Info("shutting down") // Wait for runners to finish before shutting down. // This is to make sure no orphaned runner process is left running in standalone mode. l.Wait() diff --git a/internal/envoygateway/config/loader/configloader.go b/internal/envoygateway/config/loader/configloader.go index fe3b2b9424..c35b135867 100644 --- a/internal/envoygateway/config/loader/configloader.go +++ b/internal/envoygateway/config/loader/configloader.go @@ -142,6 +142,14 @@ func (r *Loader) Errors() <-chan error { return r.hookErr } +// Logger returns the current logger, safe to call concurrently with a config +// reload replacing it. +func (r *Loader) Logger() logging.Logger { + r.cfgMu.RLock() + defer r.cfgMu.RUnlock() + return r.cfg.Logger +} + // Wait returns when success to acquire mutex, which means no hook is running. func (r *Loader) Wait() { r.hookMutex.Lock() diff --git a/release-notes/current/bug_fixes/9581-config-reload-server-logger-data-race.md b/release-notes/current/bug_fixes/9581-config-reload-server-logger-data-race.md new file mode 100644 index 0000000000..c504d6156f --- /dev/null +++ b/release-notes/current/bug_fixes/9581-config-reload-server-logger-data-race.md @@ -0,0 +1 @@ +Fixed a data race between config-reload and the standalone server's shutdown/error logging by reading the logger through a new mutex-guarded `Loader.Logger()` accessor instead of the shared `cfg.Logger` field directly.