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

kqueue: synchronously close fd in Close() #334

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions fsnotify_test.go
Expand Up @@ -68,3 +68,19 @@ func TestWatcherClose(t *testing.T) {
t.Fatal(err)
}
}

func TestOpenCloseQuickly(t *testing.T) {
name := tempMkFile(t, "")

for i := 0; i < 1000; i++ {
w := newWatcher(t)
err := w.Add(name)
if err != nil {
t.Fatal(err)
}
err = w.Close()
if err != nil {
t.Fatal(err)
}
}
}
29 changes: 16 additions & 13 deletions kqueue.go
Expand Up @@ -86,6 +86,7 @@ func (w *Watcher) Close() error {

// send a "quit" message to the reader goroutine
close(w.done)
unix.Close(w.kq)

return nil
}
Expand Down Expand Up @@ -263,15 +264,12 @@ func (w *Watcher) readEvents() {

loop:
for {
// See if there is a message on the "done" channel
select {
case <-w.done:
break loop
default:
}

// Get new events
kevents, err := read(w.kq, eventBuffer, &keventWaitTime)
// EBADF means the watcher was closed
if err != nil && err == unix.EBADF {
break
}
// EINTR is okay, the syscall was interrupted before timeout expired.
if err != nil && err != unix.EINTR {
select {
Expand Down Expand Up @@ -352,12 +350,17 @@ loop:
}

// cleanup
err := unix.Close(w.kq)
if err != nil {
// only way the previous loop breaks is if w.done was closed so we need to async send to w.Errors.
select {
case w.Errors <- err:
default:
select {
case <-w.done:
// file descriptor is already closed by w.Close()
default:
err := unix.Close(w.kq)
if err != nil && err != unix.EBADF {
// only way the previous loop breaks is if w.done was closed so we need to async send to w.Errors.
select {
case w.Errors <- err:
default:
}
}
}
close(w.Events)
Expand Down