Skip to content

Commit ed24d84

Browse files
author
sersut
committed
Solve the race condition while handling concurrent connections.
1 parent b659f28 commit ed24d84

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

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)