mirror of
https://github.com/openlibrecommunity/olcrtc.git
synced 2026-05-26 07:08:11 +00:00
fix: drain stderr filter before exit to prevent log loss on startup errors
This commit is contained in:
@@ -63,6 +63,7 @@ type failoverConfig struct {
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
logger.Error(err)
|
||||
flushStderrFilter()
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,12 @@ import (
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var stderrFilterOnce sync.Once //nolint:gochecknoglobals // process-wide stderr fd filter
|
||||
var ( //nolint:gochecknoglobals // process-wide stderr fd filter
|
||||
stderrFilterOnce sync.Once
|
||||
stderrPipeWriter *os.File
|
||||
stderrFilterDone chan struct{}
|
||||
stderrFilterActive bool
|
||||
)
|
||||
|
||||
func installStderrFilter() {
|
||||
stderrFilterOnce.Do(func() {
|
||||
@@ -30,13 +35,29 @@ func installStderrFilter() {
|
||||
_ = unix.Close(origFD)
|
||||
return
|
||||
}
|
||||
_ = writer.Close()
|
||||
stderrPipeWriter = writer
|
||||
stderrFilterDone = make(chan struct{})
|
||||
stderrFilterActive = true
|
||||
os.Stderr = os.NewFile(uintptr(unix.Stderr), "/dev/stderr")
|
||||
orig := os.NewFile(uintptr(origFD), "/dev/stderr-original")
|
||||
go copyFilteredStderr(reader, orig)
|
||||
go func() {
|
||||
defer close(stderrFilterDone)
|
||||
copyFilteredStderr(reader, orig)
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
// flushStderrFilter closes the pipe write ends so the filter goroutine
|
||||
// sees EOF and drains any buffered output before the process exits.
|
||||
func flushStderrFilter() {
|
||||
if !stderrFilterActive {
|
||||
return
|
||||
}
|
||||
_ = stderrPipeWriter.Close()
|
||||
_ = unix.Close(unix.Stderr)
|
||||
<-stderrFilterDone
|
||||
}
|
||||
|
||||
func copyFilteredStderr(reader *os.File, out io.Writer) {
|
||||
defer func() { _ = reader.Close() }()
|
||||
br := bufio.NewReader(reader)
|
||||
|
||||
@@ -3,3 +3,5 @@
|
||||
package main
|
||||
|
||||
func installStderrFilter() {}
|
||||
|
||||
func flushStderrFilter() {}
|
||||
|
||||
Reference in New Issue
Block a user