Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent a race condition where the number of connected clients could #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions lib/gserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def join
#
# +client+:: a TCPSocket instance representing the client that connected
#
# Return true to allow this client to connect, false to prevent it.
# Return true to allow this client to be served, false to prevent it.
def connecting(client)
addr = client.peeraddr
log("#{self.class} #{@host}:#{@port} client:#{addr[1]} " +
Expand All @@ -167,9 +167,9 @@ def connecting(client)
end


# Called when a client disconnects, if audition is enabled.
# Called when a client disconnects, if auditing is enabled.
#
# +clientPort+:: the port of the client that is connecting
# +clientPort+:: the port of the client that is disconnecting
def disconnecting(clientPort)
log("#{self.class} #{@host}:#{@port} " +
"client:#{clientPort} disconnect")
Expand Down Expand Up @@ -242,7 +242,7 @@ def start(maxConnections = -1)
raise "server is already running" if !stopped?
@shutdown = false
@maxConnections = maxConnections if maxConnections > 0
@@servicesMutex.synchronize {
@@servicesMutex.synchronize {
if GServer.in_service?(@port,@host)
raise "Port already in use: #{host}:#{@port}!"
end
Expand All @@ -255,14 +255,16 @@ def start(maxConnections = -1)
begin
starting if @audit
while !@shutdown
@connectionsMutex.synchronize {
while @connections.size >= @maxConnections
@connectionsCV.wait(@connectionsMutex)
end
connection = []
@connectionsMutex.synchronize {
while @connections.size >= @maxConnections
@connectionsCV.wait(@connectionsMutex)
end
@connections << connection
}
client = @tcpServer.accept
Thread.new(client) { |myClient|
@connections << Thread.current
Thread.new(client, connection) { |myClient, myConnection|
myConnection << Thread.current
begin
myPort = myClient.peeraddr[1]
serve(myClient) if !@audit or connecting(myClient)
Expand All @@ -274,7 +276,7 @@ def start(maxConnections = -1)
rescue
end
@connectionsMutex.synchronize {
@connections.delete(Thread.current)
@connections.delete(myConnection)
@connectionsCV.signal
}
disconnecting(myPort) if @audit
Expand All @@ -289,13 +291,13 @@ def start(maxConnections = -1)
rescue
end
if @shutdown
@connectionsMutex.synchronize {
while @connections.size > 0
@connectionsCV.wait(@connectionsMutex)
end
@connectionsMutex.synchronize {
while @connections.size > 0
@connectionsCV.wait(@connectionsMutex)
end
}
else
@connections.each { |c| c.raise "stop" }
@connections.each { |(c)| c&.raise "stop" }
end
@tcpServerThread = nil
@@servicesMutex.synchronize {
Expand Down