Enhance exception handling

This commit is contained in:
Fierelier 2021-04-22 05:52:00 +02:00
parent a14a80809c
commit 479c5496cf
1 changed files with 33 additions and 14 deletions

View File

@ -97,6 +97,8 @@ def closeConnection(connectionId):
pass
del connections[connectionId]
return True
return False
class outThread(threading.Thread):
def __init__(self,connectionId):
@ -122,11 +124,19 @@ class outThread(threading.Thread):
with connectionsLock: closeConnection(self.connectionId)
removeThread()
return
except:
with connectionsLock: closeConnection(self.connectionId)
except Exception as e:
closed = False
with connectionsLock:
closed = closeConnection(self.connectionId)
removeThread()
print("Thread closed - Exception:")
raise
if type(e) in [queue.Empty,ConnectionResetError,socket.timeout]:
closed = False
print("Connection closed - Connection error (" +str(type(e))+ ")")
if closed:
print("Connection closed - Exception:")
raise
class inThread(threading.Thread):
def __init__(self,connectionId):
@ -148,7 +158,7 @@ class inThread(threading.Thread):
data = connection.recv(1000)
if data == b"":
with connectionsLock: closeConnection(self.connectionId)
print("Thread closed - Client disconnected.")
print("Connection closed - Client disconnected")
removeThread()
return
data = data.decode("utf-8")
@ -166,7 +176,7 @@ class inThread(threading.Thread):
with fileLock:
if not os.path.isfile(userPath):
with connectionsLock: closeConnection(self.connectionId)
print("Thread closed - Invalid user given: " +user)
print("Connection closed - Invalid user given: " +user)
removeThread()
return
@ -176,7 +186,7 @@ class inThread(threading.Thread):
if cmd == "watch":
if cmd in config and "pass" in config[cmd] and config[cmd]["pass"] != "" and config[cmd]["pass"] != password:
with connectionsLock: closeConnection(self.connectionId)
print("Thread closed - Invalid password given for user: " +user)
print("Connection closed - Invalid password given for user: " +user)
removeThread()
return
@ -197,7 +207,7 @@ class inThread(threading.Thread):
if cmd == "broadcast":
if cmd in config and "pass" in config[cmd] and config[cmd]["pass"] != "" and config[cmd]["pass"] != password:
with connectionsLock: closeConnection(self.connectionId)
print("Thread closed - Invalid password given for user: " +user)
print("Connection closed - Invalid password given for user: " +user)
removeThread()
return
@ -214,6 +224,7 @@ class inThread(threading.Thread):
deleteList.append(connectionId)
for connectionId in deleteList:
print("Connection closed - Kicking old broadcaster")
closeConnection(connectionId)
connections[self.connectionId]["action"] = "broadcast"
@ -228,7 +239,7 @@ class inThread(threading.Thread):
data = connection.recv(bufferSize)
if data == b"":
with connectionsLock: closeConnection(self.connectionId)
print("Thread closed - Client disconnected.")
print("Connection closed - Client disconnected")
removeThread()
return
@ -240,7 +251,7 @@ class inThread(threading.Thread):
if connectionData["user"] != user: continue
accumulatedData = thread.queue.qsize() * bufferSize
if accumulatedData > maxAccumulatedData:
print("Thread closed - Too much accumulated data.")
print("Connection closed - Too much accumulated data")
closeConnection(connectionId)
removeThread()
return
@ -264,11 +275,19 @@ class inThread(threading.Thread):
with connectionsLock: closeConnection(self.connectionId)
removeThread()
return
except:
with connectionsLock: closeConnection(self.connectionId)
except Exception as e:
closed = False
with connectionsLock:
closed = closeConnection(self.connectionId)
removeThread()
print("Thread closed - Exception:")
raise
if type(e) in [queue.Empty,ConnectionResetError,socket.timeout]:
closed = False
print("Connection closed - Connection error (" +str(type(e))+ ")")
if closed:
print("Connection closed - Exception:")
raise
class debugThread(threading.Thread):
def __init__(self):