Skip to content

Commit 508ecc6

Browse files
p-datadogp
andauthored
Use Mutex#synchronize (#5098)
Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 3d0ea3c commit 508ecc6

File tree

3 files changed

+25
-43
lines changed

3 files changed

+25
-43
lines changed

lib/datadog/core/remote/worker.rb

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,49 +25,45 @@ def initialize(interval:, logger:, &block)
2525
def start
2626
logger.debug { "remote worker starting (pid: #{Process.pid})" }
2727

28-
acquire_lock
28+
@mutex.synchronize do
29+
if @stopped
30+
logger.debug('remote worker: refusing to restart after previous stop')
31+
return
32+
end
2933

30-
if @stopped
31-
logger.debug('remote worker: refusing to restart after previous stop')
32-
return
33-
end
34-
35-
return if @starting || @started
34+
return if @starting || @started
3635

37-
@starting = true
36+
@starting = true
3837

39-
thread = Thread.new { poll(@interval) }
40-
thread.name = self.class.name
41-
thread.thread_variable_set(:fork_safe, true)
42-
@thr = thread
38+
thread = Thread.new { poll(@interval) }
39+
thread.name = self.class.name
40+
thread.thread_variable_set(:fork_safe, true)
41+
@thr = thread
4342

44-
@started = true
45-
@starting = false
43+
@started = true
44+
@starting = false
45+
end
4646

4747
logger.debug { 'remote worker started' }
48-
ensure
49-
release_lock
5048
end
5149

5250
def stop
5351
logger.debug { "remote worker stopping (pid: #{Process.pid})" }
5452

55-
acquire_lock
53+
@mutex.synchronize do
54+
thread = @thr
5655

57-
thread = @thr
56+
if thread
57+
thread.kill
58+
thread.join
59+
end
5860

59-
if thread
60-
thread.kill
61-
thread.join
61+
@started = false
62+
@thr = nil
63+
@stopped = true
6264
end
6365

64-
@started = false
65-
@thr = nil
66-
@stopped = true
67-
6866
logger.debug { 'remote worker stopped' }
69-
ensure
70-
release_lock
7167
end
7268

7369
def started?
@@ -76,14 +72,6 @@ def started?
7672

7773
private
7874

79-
def acquire_lock
80-
@mutex.lock
81-
end
82-
83-
def release_lock
84-
@mutex.unlock
85-
end
86-
8775
def poll(interval)
8876
loop do
8977
break unless @mutex.synchronize { @starting || @started }

sig/datadog/core/remote/worker.rbs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ module Datadog
2323

2424
private
2525

26-
def acquire_lock: () -> void
27-
28-
def release_lock: () -> void
29-
3026
def poll: (::Float interval) -> void
3127

3228
def call: () -> void

spec/datadog/core/remote/worker_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
end
2727

2828
it 'acquire and release lock' do
29-
expect(worker).to receive(:acquire_lock).at_least(:once)
30-
expect(worker).to receive(:release_lock).at_least(:once)
29+
expect(worker.instance_variable_get('@mutex')).to receive(:synchronize).at_least(:once)
3130
worker.start
3231
end
3332

@@ -84,8 +83,7 @@
8483
end
8584

8685
it 'acquire and release lock' do
87-
expect(worker).to receive(:acquire_lock)
88-
expect(worker).to receive(:release_lock)
86+
expect(worker.instance_variable_get('@mutex')).to receive(:synchronize).at_least(:once)
8987
worker.stop
9088
end
9189
end

0 commit comments

Comments
 (0)