Websocketd: more error-proof fd removal.

master
Karchnu 2020-08-29 02:01:18 +02:00
parent a70b633c42
commit 8ede5bea3d
1 changed files with 11 additions and 10 deletions

View File

@ -83,28 +83,24 @@ class InstanceStorage
def remove_fd (fdclient : Int32)
Baguette::Log.info "closing the client:#{CRESET} #{fdclient}"
# 1. closing both the client and the service
fdservice = @switchtable[fdclient]?
if fdservice.nil?
Baguette::Log.error "client #{fdclient} already removed!"
return
end
begin
tcpfdc = @fd_to_tcpsocket[fdclient]
# 2. closing the TCP connections
tcpfdc.close unless tcpfdc.closed?
rescue e
Baguette::Log.error "1"
Baguette::Log.error "remove_fd: 1 #{e}"
end
# 3. removing the client and the service fds from the loop check
begin
@service.remove_fd (fdclient)
rescue e
Baguette::Log.error "2"
Baguette::Log.error "remove_fd: 2 #{e}"
end
# 5. removing both the client and the service from the switchtable
fdservice = @switchtable[fdclient]?
@switchtable = @switchtable.select do |fdc, fds|
fdc != fdclient && fds != fdclient
end
@ -118,18 +114,23 @@ class InstanceStorage
end
begin
unless fdservice.nil?
if fdservice.nil?
Baguette::Log.debug "client #{fdclient} aleady has its service removed"
else
service = @fd_to_ipcclient[fdservice]
service.close
@service.remove_fd (fdservice)
@fd_to_ipcclient = @fd_to_ipcclient.select do |k, v|
k != fdservice
end
@is_client = @is_client.select do |fd,v| fd != fdservice end
# perform the close at the end
# on crash, this service still is removed from the list of listened fd
service.close
end
rescue e
Baguette::Log.error "3"
Baguette::Log.error "remove_fd: 3 #{e}"
end
rescue e
Baguette::Log.error "in InstanceStorage#remove_fd: #{e}"