Skip to content

Commit

Permalink
allow stopping a notifier from within an event callback
Browse files Browse the repository at this point in the history
This was allowed pre-0.10.0 but got broken by the synchronization
of #stop with #run quitting.
  • Loading branch information
doudou authored and ioquatix committed Dec 24, 2019
1 parent 3fae270 commit 469f873
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/rb-inotify/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,13 @@ def watch(path, *flags, &callback)
# @see #process
def run
@running.synchronize do
Thread.current[:INOTIFY_RUN_THREAD] = true
@stop = false

process until @stop
end
ensure
Thread.current[:INOTIFY_RUN_THREAD] = false
end

# Stop watching for filesystem events.
Expand All @@ -237,8 +240,10 @@ def stop
@stop = true
@pipe.last.write "."

@running.synchronize do
# no-op: we just needed to wait until the lock was available
unless Thread.current[:INOTIFY_RUN_THREAD]
@running.synchronize do
# no-op: we just needed to wait until the lock was available
end
end
end

Expand Down
10 changes: 10 additions & 0 deletions spec/notifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@

expect(events.map(&:name)).to match_array(%w(one.txt two.txt))
end

it "can be stopped from within a callback" do
barriers = Array.new(3) { Concurrent::Event.new }
barrier_queue = barriers.dup
events = recording(dir, :create) { @notifier.stop }

run_thread = Thread.new { @notifier.run }
dir.join("one.txt").write("hello world")
run_thread.join
end
end

describe :fd do
Expand Down

0 comments on commit 469f873

Please sign in to comment.