]> go.fuhry.dev Git - fsnotify.git/commit
Fix deadlock in Remove (linux/inotify)
authorAaron L <aaron@bettercoder.net>
Wed, 29 Mar 2017 04:21:07 +0000 (21:21 -0700)
committerMark Bates <mark@markbates.com>
Wed, 29 Mar 2017 11:06:42 +0000 (07:06 -0400)
commit4da3e2cfbabc9f751898f250b49f2439785783a1
tree95b6c61860de89ebfaa370b07c1067405bbeefc1
parentff7bc41d4007f67e5456703c34342df4e0113f64
Fix deadlock in Remove (linux/inotify)

Several people have reported this issue where if you are using a
single goroutine to watch for fs events and you call Remove in
that goroutine it can deadlock. The cause for this is that the Remove
was made synchronous by PR #73. The reason for this was to try and
ensure that maps were no longer leaking.

In this PR: IN_IGNORE was used as the event to ensure map cleanup.
This worked fine when Remove() was called and the next event was
IN_IGNORE, but when a different event was received the main goroutine
that's supposed to be reading from the Events channel would be stuck
waiting for the sync.Cond, which would never be hit because the select
would then block waiting for someone to receive the non-IN_IGNORE event
from the channel so it could proceed to process the IN_IGNORE event that
was waiting in the queue. Deadlock :)

Removing the synchronization then created two nasty races where Remove
followed by Remove would error unnecessarily, and one where Remove
followed by an Add could result in the maps being cleaned up AFTER the
Add call which means the inotify watch is active, but our maps don't
have the values anymore. It then becomes impossible to delete the
watches via the fsnotify code since it checks it's local data before
calling InotifyRemove.

This code attempts to use IN_DELETE_SELF as a means to know when a watch
was deleted as part of an unlink(). That means that we didn't delete the
watch via the fsnotify lib and we should clean up our maps since that
watch no longer exists. This allows us to clean up the maps immediately
when calling Remove since we no longer try to synchronize cleanup
using IN_IGNORE as the sync point.

- Fix #195
- Fix #123
- Fix #115
inotify.go
inotify_test.go