Skip to content

Commit

Permalink
Merge pull request #3519 from DataDog/ivoanjo/prof-9136-remote-worker…
Browse files Browse the repository at this point in the history
…-thread-leak

[PROF-9136] Fix rare remote configuration worker thread leak
  • Loading branch information
ivoanjo authored Mar 13, 2024
2 parents 77f8b0f + e20cbaf commit f76fd72
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/datadog/core/remote/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def initialize(interval:, &block)
@thr = nil

@starting = false
@stopping = false
@started = false
@stopped = false

@interval = interval
raise ArgumentError, 'can not initialize a worker without a block' unless block
Expand All @@ -24,6 +24,11 @@ def start

acquire_lock

if @stopped
Datadog.logger.debug('remote worker: refusing to restart after previous stop')
return
end

return if @starting || @started

@starting = true
Expand All @@ -46,8 +51,6 @@ def stop

acquire_lock

@stopping = true

thread = @thr

if thread
Expand All @@ -56,8 +59,8 @@ def stop
end

@started = false
@stopping = false
@thr = nil
@stopped = true

Datadog.logger.debug { 'remote worker stopped' }
ensure
Expand Down
10 changes: 10 additions & 0 deletions spec/datadog/core/remote/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@

expect(worker.instance_variable_get(:@thr).thread_variable_get(:fork_safe)).to be true
end

it 'does not restart the worker after being stopped once' do
worker.start
expect(worker.instance_variable_get(:@started)).to be true

worker.stop

worker.start
expect(worker.instance_variable_get(:@started)).to be false
end
end

describe '#stop' do
Expand Down

0 comments on commit f76fd72

Please sign in to comment.