diff --git a/lib/rb-inotify/notifier.rb b/lib/rb-inotify/notifier.rb index 89be6f8..e653d49 100644 --- a/lib/rb-inotify/notifier.rb +++ b/lib/rb-inotify/notifier.rb @@ -50,6 +50,7 @@ def fd # @raise [SystemCallError] if inotify failed to initialize for some reason def initialize @running = Mutex.new + @stop = false @pipe = IO.pipe # JRuby shutdown sometimes runs IO finalizers before all threads finish. if RUBY_ENGINE == 'jruby' @@ -231,12 +232,12 @@ def watch(path, *flags, &callback) def run @running.synchronize do Thread.current[:INOTIFY_RUN_THREAD] = true - @stop = false process until @stop end ensure Thread.current[:INOTIFY_RUN_THREAD] = false + @stop = false end # Stop watching for filesystem events. diff --git a/spec/notifier_spec.rb b/spec/notifier_spec.rb index cd1abfd..8370151 100644 --- a/spec/notifier_spec.rb +++ b/spec/notifier_spec.rb @@ -107,6 +107,20 @@ dir.join("one.txt").write("hello world") run_thread.join(1) or raise "timeout" end + + it "can be stopped before it starts processing" do + barrier = Concurrent::Event.new + + run_thread = Thread.new do + barrier.wait + @notifier.run + end + + @notifier.stop + barrier.set + + run_thread.join(1) or raise "timeout" + end end describe :fd do