Skip to content

Commit

Permalink
Stop flush timer before closing queue
Browse files Browse the repository at this point in the history
As `Datadog::Sender#flush` adds `:flush` command to the queue, the
flush timer should be closed before the sender closes the queue to
avoid ClosedQueueError.
  • Loading branch information
abicky committed May 6, 2022
1 parent 1870ba7 commit 335af9e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/datadog/statsd/sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ def start
# to close the sender nor trying to continue to `#add` more message
# into the sender.
def stop(join_worker: true)
@flush_timer.stop if @flush_timer

message_queue = @message_queue
message_queue.close if message_queue

@flush_timer.stop if @flush_timer
sender_thread = @sender_thread
sender_thread.join if sender_thread && join_worker
end
Expand All @@ -114,10 +115,11 @@ def stop(join_worker: true)
# to close the sender nor trying to continue to `#add` more message
# into the sender.
def stop(join_worker: true)
@flush_timer.stop if @flush_timer

message_queue = @message_queue
message_queue << :close if message_queue

@flush_timer.stop if @flush_timer
sender_thread = @sender_thread
sender_thread.join if sender_thread && join_worker
end
Expand Down
4 changes: 4 additions & 0 deletions spec/statsd/sender_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@
let(:flush_interval) { 15 }

it 'stops the worker thread and the flush timer thread' do
# sleep a little to wait for the sender thread to start
sleep 0.01

expect(message_buffer).to receive(:flush)
expect do
subject.stop
end.to change { Thread.list.size }.by(-2)
Expand Down

0 comments on commit 335af9e

Please sign in to comment.