Skip to content

Commit 36b55e6

Browse files
committed
Merge pull request #4 from chef/ss/concurrency-fixes
Fix two problems with :concurrent_connections option
2 parents 62fd477 + 2387782 commit 36b55e6

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/net/ssh/multi/server.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ def session(require_session=false)
140140

141141
# Returns +true+ if the session has been opened, and the session is currently
142142
# busy (as defined by Net::SSH::Connection::Session#busy?).
143+
# Also returns false if the server has failed to connect.
143144
def busy?(include_invisible=false)
144-
session && session.busy?(include_invisible)
145+
!failed? && session && session.busy?(include_invisible)
145146
end
146147

147148
# Closes this server's session. If the session has not yet been opened,
@@ -228,4 +229,4 @@ def postprocess(readers, writers) #:nodoc:
228229
session.postprocess(listeners & readers, listeners & writers)
229230
end
230231
end
231-
end; end; end
232+
end; end; end

lib/net/ssh/multi/session.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,12 @@ def next_session(server, force=false) #:nodoc:
476476
return connection
477477
end
478478

479-
@open_connections += 1
479+
# Only increment the open_connections count if the connection
480+
# is not being forced. Incase of a force, it will already be
481+
# incremented.
482+
if !force
483+
@open_connections += 1
484+
end
480485
end
481486

482487
begin
@@ -542,6 +547,10 @@ def realize_pending_connections! #:nodoc:
542547
count = concurrent_connections ? (concurrent_connections - open_connections) : @pending_sessions.length
543548
count.times do
544549
session = @pending_sessions.pop or break
550+
# Increment the open_connections count here to prevent
551+
# creation of connection thread again before that is
552+
# incremented by the thread.
553+
@session_mutex.synchronize { @open_connections += 1 }
545554
@connect_threads << Thread.new do
546555
session.replace_with(next_session(session.server, true))
547556
end

0 commit comments

Comments
 (0)