hpcloud / tail

Go package for reading from continously updated files (tail -f)
MIT License
2.69k stars 502 forks source link

Deadlock when using fsnotify on Windows #54

Open rubenhazelaar opened 9 years ago

rubenhazelaar commented 9 years ago

Hi, I'm new to Go and have written a very simple program to watch some logs (which rotate). However although the program runs fine for some time, eventually it deadlocks with the following message:

fatal error: all goroutines are asleep - deadlock! 

The code I'm using looks like this:

t, err := tail.TailFile("path/to/some/log", tail.Config{
    Follow: true,
    ReOpen: true})
if err != nil {
    log.Fatal(err)
}

for line := range t.Lines {
    // Some action based on a log line
}

I can provide a stacktrace if necessary

BTW: I'm on Windows.

srid commented 9 years ago

What is the full code (the .go file)? Example program can be found here: https://github.com/ActiveState/tail/blob/master/cmd/gotail/gotail.go

rubenhazelaar commented 9 years ago

Here the full code, I think it has something to do with fsnotify for windows. With poll = true the problem does not occur. As you see it is much simpeler than your example, I think I implemented it correctly. Below the code also the stacktrace for the go routines.

package main

import (
    "github.com/ActiveState/tail"
    "log"
    "regexp"
)

func main() {
    t, err := tail.TailFile("path/to/log/file", tail.Config{
        Follow: true,
        ReOpen: true,
        Poll: true, // With poll = true appended the problem does NOT occur, so suspect it has something to do with fsnotify
    })
    if err != nil {
        log.Fatal(err)
    }

    r, err := regexp.Compile(`regex`)
    if err != nil {
        log.Println("There is problem with the regexp.")
        return
    }

    for line := range t.Lines {
        if r.MatchString(line.Text) == true {
            // action
        }
    }
}

Stacktrace:

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
        src/user/main.go
dcwatcher/main.go:26 +0x2e5

goroutine 5 [select]:
github.com/ActiveState/tail.(*Tail).waitForChanges(0xc08204e000, 0x0, 0x0)
        src/github.com/ActiveState/tail/tail.g
o:298 +0x922
github.com/ActiveState/tail.(*Tail).tailFileSync(0xc08204e000)
        src/github.com/ActiveState/tail/tail.g
o:265 +0xa5f
created by github.com/ActiveState/tail.TailFile
        src/github.com/ActiveState/tail/tail.g
o:113 +0x471

goroutine 12 [chan send, locked to thread]:
gopkg.in/fsnotify%2ev0.(*Watcher).readEvents(0xc082086f00)
        src/gopkg.in/fsnotify.v0/fsnotify_wind
ows.go:479 +0xbfe
created by gopkg.in/fsnotify%2ev0.NewWatcher
        src/gopkg.in/fsnotify.v0/fsnotify_wind
ows.go:148 +0x452

goroutine 13 [chan receive]:
gopkg.in/fsnotify%2ev0.(*Watcher).purgeEvents(0xc082086f00)
        src/gopkg.in/fsnotify.v0/fsnotify.go:2
1 +0x5c
created by gopkg.in/fsnotify%2ev0.NewWatcher
        src/gopkg.in/fsnotify.v0/fsnotify_wind
ows.go:149 +0x46c

goroutine 14 [select]:
github.com/ActiveState/tail/watch.func┬À001()
        src/github.com/ActiveState/tail/watch/
inotify.go:88 +0x502
created by github.com/ActiveState/tail/watch.(*InotifyFileWatcher).ChangeEvents
        src/github.com/ActiveState/tail/watch/
inotify.go:125 +0x54a
Nino-K commented 8 years ago

Hi @rubenhazelaar, I have upgraded the version of fsnotify from v0 to v1. Have you tried the newer version of tail, to see if you are still seeing this issue?

mitchellh commented 8 years ago

I'm not seeing a deadlock error but I can confirm that with a log file that is not frequently updated (maybe once per second) I am seeing tail stop sending lines after a certain period of time when using Poll: false. By setting poll to true, everything works fine.

Note that this is on Windows. On OS X everything appears to work fine with poll false.

Nino-K commented 8 years ago

Hi @mitchellh, thanks for pointing it out. Can you confirm if you have tried this on the latest master, with fsnotify v1?

It is a known issue that I'm currently looking into it. I was hoping updating fsnotify to v1 would have rectified it as it originally appeared to be from fsnotify.

sunshine69 commented 4 years ago

This seems not to be resolved yet. I have just hit the same issue and currently watching the poll options to see if it is happy.