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 <aayushdixit924@gmail.com>
This commit is contained in:
Aayush7352 2026-07-06 01:35:47 +05:30
parent 92e768f541
commit 21b363c2bb
2 changed files with 6 additions and 6 deletions

View File

@ -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 {

View File

@ -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
}