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

File handle leak on OS X #59

Closed
neelance opened this issue Jan 27, 2015 · 8 comments
Closed

File handle leak on OS X #59

neelance opened this issue Jan 27, 2015 · 8 comments

Comments

@neelance
Copy link

The following code quickly runs into a panic: too many open files on OS X, even though the documentation says that Close removes all watches.

package main

import "gopkg.in/fsnotify.v1"

func main() {
    for {
        println("loop")

        w, err := fsnotify.NewWatcher()
        if err != nil {
            panic(err)
        }

        go func() {
            for range w.Events {
            }
        }()
        go func() {
            for range w.Errors {
            }
        }()

        if err := w.Add("test.go"); err != nil {
            panic(err)
        }

        // uncomment this to make the leak go away
        // if err := w.Remove("test.go"); err != nil {
        //  panic(err)
        // }

        if err := w.Close(); err != nil {
            panic(err)
        }
    }
}
@nathany
Copy link
Contributor

nathany commented Jan 27, 2015

Thanks for reporting this.

@nathany nathany added the linux label Feb 7, 2015
@nathany
Copy link
Contributor

nathany commented Feb 7, 2015

@PieterD I'm looking this on OS X and BSD, but it also fails on Linux:

using import "github.com/go-fsnotify/fsnotify"

panic: inotify_init: too many open files

@PieterD
Copy link
Contributor

PieterD commented Feb 7, 2015

This would cause a tremendous amount of problems. With my next pull, I've got w.Close waiting until the goroutine is done. That will fix this.

@nathany
Copy link
Contributor

nathany commented Feb 7, 2015

Excellent.

kqueue:
This is interesting, as Close just calls Remove for all watches. Another race condition?
Close is ignoring the errors that Remove sends back, in this case: "bad file descriptor"

@PieterD
Copy link
Contributor

PieterD commented Feb 7, 2015

If Close just removes all watches but the goroutine blocks on something, the goroutines will start heaping up as you keep adding more goroutines, all blocking, all keeping open files.

They don't even have to block; if you create the next goroutine without waiting for the last one to finish, you could create a thousand before the first goroutine has a chance to clean up.

nathany added a commit that referenced this issue Feb 7, 2015
sending done would close w.kq before Remove had a chance to remove the watches with EV_DELETE, resulting in a file handle leak.

ref #59

also make Close() report last error returned by Remove.
nathany added a commit that referenced this issue Feb 7, 2015
sending done would close w.kq before Remove had a chance to remove the watches with EV_DELETE, resulting in a file handle leak.

ref #59

also make Close() report the first error returned by Remove and abort.
nathany added a commit that referenced this issue Feb 7, 2015
sending done would close w.kq before Remove had a chance to remove the watches with EV_DELETE, resulting in a file handle leak.

ref #59

also make Close() report the first error returned by Remove and continue.
nathany added a commit that referenced this issue Feb 7, 2015
sending done would close w.kq before Remove had a chance to remove the watches with EV_DELETE, resulting in a file handle leak.

ref #59

also make Close() report the first error returned by Remove and continue.

closes #65
@PieterD
Copy link
Contributor

PieterD commented Feb 8, 2015

Confirmed that https://github.com/go-fsnotify/fsnotify/pull/66 fixes this for inotify. For OS X, I suggest making sure Close waits until the goroutine is stopped before returning.

@nathany
Copy link
Contributor

nathany commented Feb 8, 2015

Thanks @PieterD. I've already merged a fix for OS X, but I have yet to tag a new release. Tomorrow once the epoll stuff is in.

@CrazySherman
Copy link

any update on linux with this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants