Fix accumulating threads

This commit is contained in:
Fierelier 2021-04-14 05:12:35 +02:00
parent 19717d1094
commit e2c7a7f9a1

View File

@ -101,28 +101,33 @@ class inThread(threading.Thread):
self.connection = connection self.connection = connection
self.address = address self.address = address
def closeThread(self): def closeThread(self,closeConnection = True):
with threadsLock: with threadsLock:
for thread in outThreads: if closeConnection:
thread = outThreads[thread] for thread in outThreads:
if thread.user == self.user: thread = outThreads[thread]
try: if thread.user == self.user:
thread.connection.close() try:
except Exception as e: thread.connection.close()
print("closing a connection failed: " +str(e)) except Exception as e:
pass print("closing a connection failed: " +str(e))
pass
try: try:
self.connection.close() self.connection.close()
except Exception as e: except Exception as e:
print("closing a connection failed: " +str(e)) print("closing a connection failed: " +str(e))
pass pass
del inThreads[str(self.threadId)] del inThreads[str(self.threadId)]
def run(self): def run(self):
try: try:
global threadId global threadId
data = self.connection.recv(1000).decode("utf-8") data = self.connection.recv(1000)
if data == b"":
self.closeThread()
return
data = data.decode("utf-8")
while data[-1] == " ": data = data[:-1] while data[-1] == " ": data = data[:-1]
args = commandToList(data) args = commandToList(data)
cmd = args.pop(0) cmd = args.pop(0)
@ -149,8 +154,10 @@ class inThread(threading.Thread):
with threadsLock: with threadsLock:
thread = outThread(self.threadId,self.connection,self.address,self.user) thread = outThread(self.threadId,self.connection,self.address,self.user)
outThreads[str(threadId)] = thread outThreads[str(self.threadId)] = thread
thread.start() thread.start()
self.closeThread(False)
return return
if cmd == "broadcast": if cmd == "broadcast":
@ -160,6 +167,9 @@ class inThread(threading.Thread):
while True: while True:
data = self.connection.recv(bufferSize) data = self.connection.recv(bufferSize)
if data == b"":
self.closeThread()
return
with threadsLock: with threadsLock:
for thread in outThreads: for thread in outThreads:
thread = outThreads[thread] thread = outThreads[thread]