guard / rb-inotify

A thorough inotify wrapper for Ruby using FFI.
MIT License
312 stars 64 forks source link

Prevent race condition in Notifier#run / #stop #100

Closed jonathanhefner closed 4 years ago

jonathanhefner commented 4 years ago

There is a potential race condition when a notifier is run in a new thread and then immediately stopped in the original thread. If Notifier#stop sets @stop = true before Notifier#run sets @stop = false, then Notifier#run will loop until Notifier#stop is called again. Furthermore, if Notifier#run acquires the @running mutex before Notifier#stop does (but after Notifier#stop sets @stop = true), then Notifier#stop will get stuck waiting for the mutex while Notifier#run infinitely loops.

This commit modifies Notifier#run to assume @stop == false when it begins, and to reset @stop = false only after its loop terminates, thus eliminating the race.

coveralls commented 4 years ago

Coverage Status

Coverage decreased (-0.009%) to 80.374% when pulling 83abe4d5ecab7831bf3cc962e435aaa317819f93 on jonathanhefner:notifier-run-stop-race-condition into 3cee3331033a9d2cb642076de8aefac3bf0f4e3b on guard:master.

ioquatix commented 4 years ago

Thanks for your effort to improve this.