Commit 7394c98d by Arve Knudsen Committed by GitHub

MuxWriter: Handle error for already closed file (#31119)

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
parent 5c9a10d4
...@@ -57,8 +57,8 @@ func (l *MuxWriter) Write(b []byte) (int, error) { ...@@ -57,8 +57,8 @@ func (l *MuxWriter) Write(b []byte) (int, error) {
// set os.File in writer. // set os.File in writer.
func (l *MuxWriter) setFD(fd *os.File) error { func (l *MuxWriter) setFD(fd *os.File) error {
if l.fd != nil { if l.fd != nil {
if err := l.fd.Close(); err != nil { if err := l.fd.Close(); err != nil && !errors.Is(err, os.ErrClosed) {
return err return fmt.Errorf("closing old file in MuxWriter failed: %w", err)
} }
} }
...@@ -143,16 +143,18 @@ func (w *FileLogWriter) lineCounter() (int, error) { ...@@ -143,16 +143,18 @@ func (w *FileLogWriter) lineCounter() (int, error) {
count := 0 count := 0
for { for {
c, err := r.Read(buf) c, err := r.Read(buf)
count += bytes.Count(buf[:c], []byte{'\n'}) if err != nil {
switch { if errors.Is(err, io.EOF) {
case errors.Is(err, io.EOF): if err := r.Close(); err != nil && !errors.Is(err, os.ErrClosed) {
if err := r.Close(); err != nil { return 0, fmt.Errorf("closing %q failed: %w", w.Filename, err)
return count, err
} }
return count, nil return count, nil
case err != nil:
return count, err
} }
return 0, err
}
count += bytes.Count(buf[:c], []byte{'\n'})
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment