Skip to content

Commit 5daffe1

Browse files
committed
Merge pull request #6 from mighe/master
Dereferenceable mutex race condition
2 parents ac048f4 + a38ce1f commit 5daffe1

File tree

7 files changed

+14
-5
lines changed

7 files changed

+14
-5
lines changed

lib/concurrent/agent.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def initialize(initial, opts = {})
2828
@rescuers = []
2929
@validator = nil
3030
@timeout = opts[:timeout] || TIMEOUT
31+
init_mutex
3132
set_deref_options(opts)
3233
end
3334

lib/concurrent/contract.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Contract
77

88
def initialize(opts = {})
99
@state = :pending
10+
init_mutex
1011
set_deref_options(opts)
1112
end
1213

lib/concurrent/dereferenceable.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ module Dereferenceable
2121
# returning the value returned from the proc (default: `nil`)
2222
def set_deref_options(opts = {})
2323
mutex.synchronize do
24-
@dup_on_deref = opts[:dup_on_deref] || opts[:dup] || false
25-
@freeze_on_deref = opts[:freeze_on_deref] || opts[:freeze] || false
24+
@dup_on_deref = opts[:dup_on_deref] || opts[:dup]
25+
@freeze_on_deref = opts[:freeze_on_deref] || opts[:freeze]
2626
@copy_on_deref = opts[:copy_on_deref] || opts[:copy]
2727
@do_nothing_on_deref = ! (@dup_on_deref || @freeze_on_deref || @copy_on_deref)
2828
end
@@ -33,7 +33,7 @@ def set_deref_options(opts = {})
3333
def value
3434
return nil if @value.nil?
3535
return @value if @do_nothing_on_deref
36-
return mutex.synchronize do
36+
mutex.synchronize do
3737
value = @value
3838
value = @copy_on_deref.call(value) if @copy_on_deref
3939
value = value.dup if @dup_on_deref
@@ -45,9 +45,12 @@ def value
4545

4646
protected
4747

48-
# @private
4948
def mutex # :nodoc:
50-
@mutex ||= Mutex.new
49+
@mutex
50+
end
51+
52+
def init_mutex
53+
@mutex = Mutex.new
5154
end
5255
end
5356
end

lib/concurrent/future.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Future
1212
include UsesGlobalThreadPool
1313

1414
def initialize(*args, &block)
15+
init_mutex
1516
unless block_given?
1617
@state = :fulfilled
1718
else

lib/concurrent/promise.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def initialize(*args, &block)
4141
@children = []
4242
@rescuers = []
4343

44+
init_mutex
4445
realize(*args) if root?
4546
end
4647

lib/concurrent/scheduled_task.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def initialize(schedule_time, opts = {}, &block)
2929
@state = :pending
3030
@schedule_time.freeze
3131
@task = block
32+
init_mutex
3233
set_deref_options(opts)
3334

3435
@thread = Thread.new{ work }

lib/concurrent/timer_task.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def initialize(opts = {}, &block)
198198
@run_now = opts[:now] || opts[:run_now] || false
199199

200200
@task = block
201+
init_mutex
201202
set_deref_options(opts)
202203
end
203204

0 commit comments

Comments
 (0)