Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug to leak non-repeating timer watchers #1864

Merged
merged 2 commits into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/fluent/plugin_helper/event_loop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ def event_loop_attach(watcher)
end
end

def event_loop_detach(watcher)
if watcher.attached?
watcher.detach
end
@_event_loop_mutex.synchronize do
@_event_loop_attached_watchers.delete(watcher)
end
end

def event_loop_wait_until_start
sleep(0.1) until event_loop_running?
end
Expand Down
15 changes: 8 additions & 7 deletions lib/fluent/plugin_helper/timer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#

require 'fluent/plugin_helper/event_loop'
require 'set'

module Fluent
module PluginHelper
Expand All @@ -33,7 +34,8 @@ def timer_execute(title, interval, repeat: true, &block)
raise ArgumentError, "BUG: title must be a symbol" unless title.is_a? Symbol
raise ArgumentError, "BUG: block not specified for callback" unless block_given?
checker = ->(){ @_timer_running }
timer = TimerWatcher.new(title, interval, repeat, log, checker, &block)
detacher = ->(watcher){ event_loop_detach(watcher) }
timer = TimerWatcher.new(title, interval, repeat, log, checker, detacher, &block)
@_timers << title
event_loop_attach(timer)
timer
Expand All @@ -45,7 +47,7 @@ def timer_running?

def initialize
super
@_timers = []
@_timers ||= Set.new
end

def start
Expand All @@ -60,16 +62,17 @@ def stop

def terminate
super
@_timers = []
@_timers = nil
end

class TimerWatcher < Coolio::TimerWatcher
def initialize(title, interval, repeat, log, checker, &callback)
def initialize(title, interval, repeat, log, checker, detacher, &callback)
@title = title
@callback = callback
@repeat = repeat
@log = log
@checker = checker
@detacher = detacher
super(interval, repeat)
end

Expand All @@ -81,9 +84,7 @@ def on_timer
detach
@log.error "Timer detached.", title: @title
ensure
if attached?
detach unless @repeat
end
@detacher.call(self) unless @repeat
end
end
end
Expand Down