File tree Expand file tree Collapse file tree 2 files changed +40
-2
lines changed
lib/concurrent-ruby/concurrent Expand file tree Collapse file tree 2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change 5
5
require 'concurrent/configuration'
6
6
require 'concurrent/errors'
7
7
require 'concurrent/re_include'
8
+ require 'concurrent/utility/monotonic_time'
8
9
9
10
module Concurrent
10
11
@@ -772,8 +773,17 @@ def wait_until_resolved(timeout)
772
773
@Lock . synchronize do
773
774
@Waiters . increment
774
775
begin
775
- unless resolved?
776
- @Condition . wait @Lock , timeout
776
+ if timeout
777
+ start = Concurrent . monotonic_time
778
+ until resolved?
779
+ break if @Condition . wait ( @Lock , timeout ) == nil # nil means timeout
780
+ timeout -= ( Concurrent . monotonic_time - start )
781
+ break if timeout <= 0
782
+ end
783
+ else
784
+ until resolved?
785
+ @Condition . wait ( @Lock , timeout )
786
+ end
777
787
end
778
788
ensure
779
789
# JRuby may raise ConcurrencyError
Original file line number Diff line number Diff line change @@ -754,4 +754,32 @@ def behaves_as_delay(delay, value)
754
754
specify 'zip_futures_over' do
755
755
expect ( zip_futures_over ( [ 1 , 2 ] ) { |v | v . succ } . value! ) . to eq [ 2 , 3 ]
756
756
end
757
+
758
+ describe 'value!' do
759
+ %w[ with without ] . each do |timeout |
760
+ it "does not return spuriously #{ timeout } timeout" do
761
+ # https://github.com/ruby-concurrency/concurrent-ruby/issues/1015
762
+ trapped = false
763
+ original_handler = Signal . trap ( :SIGHUP ) { trapped = true }
764
+ begin
765
+ task = Concurrent ::Promises . future { sleep }
766
+ main = Thread . current
767
+ t = Thread . new do
768
+ Thread . pass until main . stop?
769
+ Process . kill ( :SIGHUP , Process . pid )
770
+ sleep 0.1
771
+ main . raise "Done"
772
+ end
773
+ expect {
774
+ timeout == 'with' ? task . value! ( 3600 ) : task . value!
775
+ expect ( task ) . to be_resolved # fail if #value! returned
776
+ } . to raise_error ( RuntimeError , "Done" )
777
+ expect ( trapped ) . to be true
778
+ t . join
779
+ ensure
780
+ Signal . trap ( :SIGHUP , original_handler )
781
+ end
782
+ end
783
+ end
784
+ end
757
785
end
You can’t perform that action at this time.
0 commit comments