From 6fdad9c547bd481ceb752177726d991332b1d82c Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 28 Oct 2024 07:25:55 -0400 Subject: [PATCH] handle the case of missing thread --- lib/datadog/di/probe_notifier_worker.rb | 8 ++++--- spec/datadog/di/probe_notifier_worker_spec.rb | 24 +++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/datadog/di/probe_notifier_worker.rb b/lib/datadog/di/probe_notifier_worker.rb index 0f872a91092..98e49e5f0f7 100644 --- a/lib/datadog/di/probe_notifier_worker.rb +++ b/lib/datadog/di/probe_notifier_worker.rb @@ -91,10 +91,12 @@ def start def stop(timeout = 1) @stop_requested = true wake.signal - unless thread&.join(timeout) - thread.kill + if thread + unless thread.join(timeout) + thread.kill + end + @thread = nil end - @thread = nil end # Waits for background thread to send pending notifications. diff --git a/spec/datadog/di/probe_notifier_worker_spec.rb b/spec/datadog/di/probe_notifier_worker_spec.rb index 147322527a8..3e95947437f 100644 --- a/spec/datadog/di/probe_notifier_worker_spec.rb +++ b/spec/datadog/di/probe_notifier_worker_spec.rb @@ -43,13 +43,27 @@ end describe '#stop' do - before do - worker.start + context 'worker is running' do + before do + worker.start + end + + it 'stops the thread' do + worker.stop + expect(worker.send(:thread)).to be nil + end end - it 'stops the thread' do - worker.stop - expect(worker.send(:thread)).to be nil + context 'worker is not running' do + before do + expect(worker.send(:thread)).to be nil + end + + it 'does nothing and raises no exceptions' do + expect do + worker.stop + end.not_to raise_error + end end end