Skip to content

Commit ef0b042

Browse files
phemmerDaniel Ayvar
authored andcommitted
ensure the watcher has started before we read (influxdata#2)
...otherwise we can run into the scenario where: On start we read the contents of the file, we hit EOF, and start watching for changes. But in between the EOF and start watching, the file is updated. But since the watcher wasn't started, we don't notice.
1 parent ba755e4 commit ef0b042

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

tail.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ func (tail *Tail) tailFileSync() {
293293

294294
tail.openReader()
295295

296+
if err := tail.watchChanges(); err != nil {
297+
tail.Killf("Error watching for changes on %s: %s", tail.Filename, err)
298+
return
299+
}
300+
296301
// Read line by line.
297302
for {
298303
// do not seek in named pipes
@@ -368,19 +373,25 @@ func (tail *Tail) tailFileSync() {
368373
}
369374
}
370375

376+
// watchChanges ensures the watcher is running.
377+
func (tail *Tail) watchChanges() error {
378+
if tail.changes != nil {
379+
return nil
380+
}
381+
pos, err := tail.file.Seek(0, io.SeekCurrent)
382+
if err != nil {
383+
return err
384+
}
385+
tail.changes, err = tail.watcher.ChangeEvents(&tail.Tomb, pos)
386+
return err
387+
}
388+
371389
// waitForChanges waits until the file has been appended, deleted,
372390
// moved or truncated. When moved or deleted - the file will be
373391
// reopened if ReOpen is true. Truncated files are always reopened.
374392
func (tail *Tail) waitForChanges() error {
375-
if tail.changes == nil {
376-
pos, err := tail.file.Seek(0, io.SeekCurrent)
377-
if err != nil {
378-
return err
379-
}
380-
tail.changes, err = tail.watcher.ChangeEvents(&tail.Tomb, pos)
381-
if err != nil {
382-
return err
383-
}
393+
if err := tail.watchChanges(); err != nil {
394+
return err
384395
}
385396

386397
select {

0 commit comments

Comments
 (0)