Skip to content

Commit 517cb79

Browse files
phemmerdanielnelson
authored andcommitted
ensure the watcher has started before we read (#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 faf842b commit 517cb79

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

tail.go

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

251251
tail.openReader()
252252

253+
if err := tail.watchChanges(); err != nil {
254+
tail.Killf("Error watching for changes on %s: %s", tail.Filename, err)
255+
return
256+
}
257+
253258
var offset int64
254259
var err error
255260

@@ -331,19 +336,25 @@ func (tail *Tail) tailFileSync() {
331336
}
332337
}
333338

339+
// watchChanges ensures the watcher is running.
340+
func (tail *Tail) watchChanges() error {
341+
if tail.changes != nil {
342+
return nil
343+
}
344+
pos, err := tail.file.Seek(0, os.SEEK_CUR)
345+
if err != nil {
346+
return err
347+
}
348+
tail.changes, err = tail.watcher.ChangeEvents(&tail.Tomb, pos)
349+
return err
350+
}
351+
334352
// waitForChanges waits until the file has been appended, deleted,
335353
// moved or truncated. When moved or deleted - the file will be
336354
// reopened if ReOpen is true. Truncated files are always reopened.
337355
func (tail *Tail) waitForChanges() error {
338-
if tail.changes == nil {
339-
pos, err := tail.file.Seek(0, os.SEEK_CUR)
340-
if err != nil {
341-
return err
342-
}
343-
tail.changes, err = tail.watcher.ChangeEvents(&tail.Tomb, pos)
344-
if err != nil {
345-
return err
346-
}
356+
if err := tail.watchChanges(); err != nil {
357+
return err
347358
}
348359

349360
select {

0 commit comments

Comments
 (0)