Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continious truncate is not handled correctly #62

Open
akamensky opened this issue Dec 18, 2023 · 1 comment
Open

Continious truncate is not handled correctly #62

akamensky opened this issue Dec 18, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@akamensky
Copy link

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

@akamensky akamensky added the bug Something isn't working label Dec 18, 2023
@edx-sayed-salem
Copy link

it may have to do with this condition:

tail/watch/inotify.go

Lines 124 to 129 in ba755e4

if prevSize > 0 && prevSize > fw.Size {
changes.NotifyTruncated()
} else {
changes.NotifyModified()
}

So it might be when the file changes but remains the same length, it is not considered truncated. Maybe using modification time would help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants