nxadm/tail

Continious truncate is not handled correctly

akamensky opened this issue · 1 comments

Describe the bug
Simple code:

func main() {
	if len(os.Args) != 2 {
		panic("not enough arguments")
	}

	filename := os.Args[1]

	t, err := tail.TailFile(filename, tail.Config{Follow: true, ReOpen: true})
	if err != nil {
		panic(err)
	}

	for line := range t.Lines {
		fmt.Println(line.Text)
	}
}

When used in conjunction with continuous truncation (>) as opposed to appending (>>) only reacts to the first event, ignore all following ones. Simple test case:

$ while true; do echo "$(date)" > watched.log; sleep 1; done

It will truncate file every second before writing a single line into it.

Expected behaviour
The code correctly detects the truncation events and re-opens the file accordingly. So the output would look something like this:

Mon Dec 18 04:23:44 PM UTC 2023
2023/12/18 16:23:45 Re-opening truncated file watched.log ...
2023/12/18 16:23:45 Successfully reopened truncated watched.log
Mon Dec 18 04:23:45 PM UTC 2023
2023/12/18 16:23:46 Re-opening truncated file watched.log ...
2023/12/18 16:23:46 Successfully reopened truncated watched.log
Mon Dec 18 04:23:46 PM UTC 2023
...

Actual behaviour
It detects first event, and reads a single line after that ignoring everything else:

2023/12/18 16:31:45 Waiting for watched.log to appear...
Mon Dec 18 04:31:48 PM UTC 2023

To Reproduce
See steps above. I've tried with Poll:true to no avail.

System information

  • 1.4.11
  • Fedora 37
  • amd64

Additional context