Skip to content

Commit

Permalink
in_tail: Skip setup failed watcher to avoid resource leak and log blo…
Browse files Browse the repository at this point in the history
…at. ref #1713
  • Loading branch information
repeatedly committed Nov 9, 2017
1 parent d4e2851 commit e80921e
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions lib/fluent/plugin/in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ class TailInput < Fluent::Plugin::Input

helpers :timer, :event_loop, :parser, :compat_parameters

class WatcherSetupError < StandardError
def initialize(msg)
@message = msg
end

def to_s
@message
end
end

FILE_PERMISSION = 0644

def initialize
Expand Down Expand Up @@ -250,6 +260,12 @@ def setup_watcher(path, pe)
event_loop_attach(watcher.stat_trigger)
end
tw
rescue => e
if tw
tw.detach
tw.close
end
raise e
end

def start_watchers(paths)
Expand All @@ -266,7 +282,13 @@ def start_watchers(paths)
end
end

@tails[path] = setup_watcher(path, pe)
begin
tw = setup_watcher(path, pe)
rescue WatcherSetupError => e
log.warn "Skip #{path} because unexpected setup error happens: #{e}"
next
end
@tails[path] = tw
}
end

Expand Down Expand Up @@ -480,8 +502,8 @@ def attach
end

def detach
@timer_trigger.detach if @enable_watch_timer && @timer_trigger.attached?
@stat_trigger.detach if @stat_trigger.attached?
@timer_trigger.detach if @enable_watch_timer && @timer_trigger && @timer_trigger.attached?
@stat_trigger.detach if @stat_trigger && @stat_trigger.attached?
@io_handler.on_notify if @io_handler
end

Expand Down Expand Up @@ -698,6 +720,9 @@ def open
io = Fluent::FileWrapper.open(@watcher.path)
io.seek(@watcher.pe.read_pos + @fifo.bytesize)
io
rescue RangeError
io.close if io
raise WatcherSetupError, "seek error with #{@watcher.path}: file position = #{@watcher.pe.read_pos.to_s(16)}, reading bytesize = #{@fifo.bytesize.to_s(16)}"
rescue Errno::ENOENT
nil
end
Expand All @@ -715,6 +740,9 @@ def with_io
@io ||= open
yield @io
end
rescue WatcherSetupError => e
close
raise e
rescue
@watcher.log.error $!.to_s
@watcher.log.error_backtrace
Expand Down

0 comments on commit e80921e

Please sign in to comment.