Skip to content

Commit

Permalink
in_tail: Prevent the thread-unsafe method from being called concurrently
Browse files Browse the repository at this point in the history
Essentially there is a concurrency bug in the implementation of `in_tail`.

While `IOHandler` is not designed to be thread-safe, its method `on_notify`
can be called by multiple threads concurrently. In particular, consider the
following situation:

 1. TailInput calls `on_notify` on detach/shutdown (main thread)
 2. In the same time, StatWatcher triggers `on_notify` on filesystem events
    (eventloop thread)

This patch fixes this issue by introducing a mutex, which prevent multiple
threads from entering into the critical session simultaneously.
  • Loading branch information
Fujimoto Seiji committed Mar 2, 2018
1 parent 9428766 commit 2867f0a
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/fluent/plugin/in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -678,10 +678,15 @@ def initialize(watcher, &receive_lines)
@iobuf = ''.force_encoding('ASCII-8BIT')
@lines = []
@io = nil
@notify_mutex = Mutex.new
@watcher.log.info "following tail of #{@watcher.path}"
end

def on_notify
@notify_mutex.synchronize { handle_notify }
end

def handle_notify
with_io do |io|
begin
read_more = false
Expand Down

0 comments on commit 2867f0a

Please sign in to comment.