Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 4524db2

Browse files
committed
logger: fix follow logs for max-file=1
In case jsonlogfile is used with max-file=1 and max-size set, the log rotation is not perfomed; instead, the log file is closed and re-open with O_TRUNC. This situation is not handled by the log reader in follow mode, leading to an issue of log reader being stuck forever. This situation (file close/reopen) could be handled in waitRead(), but fsnotify library chose to not listen to or deliver this event (IN_CLOSE_WRITE in inotify lingo). So, we have to handle this by checking the file size upon receiving io.EOF from the log reader, and comparing the size with the one received earlier. In case the new size is less than the old one, the file was truncated and we need to seek to its beginning. Fixes #39235. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com> (cherry picked from commit 9cd24ba6057fa479918e9bce4e2c0554ec991394) Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com> Upstream-commit: 1e13f66fbb59fc17327b86f372c1dd65ce756daf Component: engine
1 parent fdfe818 commit 4524db2

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

  • components/engine/daemon/logger/loggerutils

components/engine/daemon/logger/loggerutils/logfile.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func compressFile(fileName string, lastTimestamp time.Time) {
265265
compressWriter := gzip.NewWriter(outFile)
266266
defer compressWriter.Close()
267267

268-
// Add the last log entry timestramp to the gzip header
268+
// Add the last log entry timestamp to the gzip header
269269
extra := rotateFileMetadata{}
270270
extra.LastTime = lastTimestamp
271271
compressWriter.Header.Extra, err = json.Marshal(&extra)
@@ -614,11 +614,25 @@ func followLogs(f *os.File, logWatcher *logger.LogWatcher, notifyRotate chan int
614614
}
615615
}
616616

617+
oldSize := int64(-1)
617618
handleDecodeErr := func(err error) error {
618619
if errors.Cause(err) != io.EOF {
619620
return err
620621
}
621622

623+
// Handle special case (#39235): max-file=1 and file was truncated
624+
st, stErr := f.Stat()
625+
if stErr == nil {
626+
size := st.Size()
627+
defer func() { oldSize = size }()
628+
if size < oldSize { // truncated
629+
f.Seek(0, 0)
630+
return nil
631+
}
632+
} else {
633+
logrus.WithError(stErr).Warn("logger: stat error")
634+
}
635+
622636
for {
623637
err := waitRead()
624638
if err == nil {

0 commit comments

Comments
 (0)