Method: Fluent::PluginHelper::Thread#thread_create

Defined in:
lib/fluent/plugin_helper/thread.rb

#thread_create(title) ⇒ Object

Ruby 2.2.3 or earlier (and all 2.1.x) cause bug about Threading (“Stack consistency error”)

by passing splatted argument to `yield`

bugs.ruby-lang.org/issues/11027 We can enable to pass arguments after expire of Ruby 2.1 (& older 2.2.x) def thread_create(title, *args)

Thread.new(*args) do |*t_args|
  yield *t_args

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fluent/plugin_helper/thread.rb', line 65

def thread_create(title)
  raise ArgumentError, "BUG: title must be a symbol" unless title.is_a? Symbol
  raise ArgumentError, "BUG: callback not specified" unless block_given?
  m = Mutex.new
  m.lock
  thread = ::Thread.new do
    m.lock # run thread after that thread is successfully set into @_threads
    m.unlock
    thread_exit = false
    ::Thread.current[:_fluentd_plugin_helper_thread_title] = title
    ::Thread.current[:_fluentd_plugin_helper_thread_started] = true
    ::Thread.current[:_fluentd_plugin_helper_thread_running] = true
    begin
      yield
      thread_exit = true
    rescue Exception => e
      log.warn "thread exited by unexpected error", plugin: self.class, title: title, error: e
      thread_exit = true
      raise
    ensure
      @_threads_mutex.synchronize do
        @_threads.delete(::Thread.current.object_id)
      end
      ::Thread.current[:_fluentd_plugin_helper_thread_running] = false
      if ::Thread.current.alive? && !thread_exit
        log.warn "thread doesn't exit correctly (killed or other reason)", plugin: self.class, title: title, thread: ::Thread.current, error: $!
      end
    end
  end
  thread.abort_on_exception = true
  thread.name = title.to_s if thread.respond_to?(:name)
  @_threads_mutex.synchronize do
    @_threads[thread.object_id] = thread
  end
  m.unlock
  thread
end