Skip to content
Open
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
2 changes: 2 additions & 0 deletions lib/datadog/statsd/sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ def start
# start background thread
@sender_thread = @thread_class.new(&method(:send_loop))
@sender_thread.name = "Statsd Sender" unless Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.3')
# advise multi-threaded app servers to ignore this thread for the purposes of fork safety warnings
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a link to Puma, so we know who's reading this variable?
Something like: https://github.com/puma/puma/blob/5937d8953a154d69cab13887cc361eef9d7df2a4/lib/puma/cluster.rb#L374

@sender_thread.thread_variable_set(:fork_safe, true)
rescue ThreadError => e
@logger.debug { "Statsd: Failed to start sender thread: #{e.message}" } if @logger
@mx.synchronize { @done = true }
Expand Down
11 changes: 9 additions & 2 deletions spec/statsd/sender_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
|thds| thds.any? { |t| t.name == "Statsd Sender" }
}
end

it 'marks the sender thread as fork safe' do
subject.start
expect(Thread.list).to satisfy {
|thds| thds.any? { |t| t.name == "Statsd Sender" && t.thread_variable_get(:fork_safe) }
}
end
end

context 'when the sender is started' do
Expand Down Expand Up @@ -204,9 +211,9 @@

let(:thread_class) do
if Thread.instance_methods.include?(:name=)
fake_thread = instance_double(Thread, { "alive?" => true, "name=" => true, "join" => true })
fake_thread = instance_double(Thread, { "alive?" => true, "name=" => true, "join" => true, "thread_variable_set" => true })
else
fake_thread = instance_double(Thread, { "alive?" => true, "join" => true })
fake_thread = instance_double(Thread, { "alive?" => true, "join" => true, "thread_variable_set" => true })
end
class_double(Thread, new: fake_thread)
end
Expand Down