From 21b363c2bbb40e9cb80dfe02102761e44df9e64a Mon Sep 17 00:00:00 2001 From: Aayush7352 Date: Mon, 6 Jul 2026 01:35:47 +0530 Subject: [PATCH] fix(server): address review feedback on SIGHUP log reopening - #5721 Hold the output file mutex across the whole open/swap/close sequence in log.SetOutputFile, so a concurrent call cannot close the file the logger is currently writing to. Also deregister the signal channel when the signaller goroutine exits. Signed-off-by: Aayush7352 --- cmd/signaller_unix.go | 1 + log/log.go | 11 +++++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/signaller_unix.go b/cmd/signaller_unix.go index 85308293e..52fb5494f 100644 --- a/cmd/signaller_unix.go +++ b/cmd/signaller_unix.go @@ -27,6 +27,7 @@ func startSignaller(ctx context.Context) func() error { return func() error { var sigChan = make(chan os.Signal, 1) signal.Notify(sigChan, triggerScanSignal, syscall.SIGHUP) + defer signal.Stop(sigChan) for { select { diff --git a/log/log.go b/log/log.go index 4cd3e0c79..7e9d30dda 100644 --- a/log/log.go +++ b/log/log.go @@ -166,18 +166,17 @@ func SetOutputFile(path string) error { if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil { return fmt.Errorf("creating log file directory: %w", err) } + outputFileMu.Lock() + defer outputFileMu.Unlock() f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { return err } SetOutput(f) - outputFileMu.Lock() - prev := outputFile - outputFile = f - outputFileMu.Unlock() - if prev != nil { - _ = prev.Close() + if outputFile != nil { + _ = outputFile.Close() } + outputFile = f return nil }