Use with lock: instead of lock.acquire() and lock.release()

This commit is contained in:
Fierelier 2021-04-09 16:45:32 +02:00
parent 6591a01459
commit 7a1ec5d0ba
4 changed files with 69 additions and 89 deletions

View File

@ -61,9 +61,8 @@ def getModlist(path):
return modList
def triggerEvent(event,*args,**kwargs):
eventHandlersLock.acquire()
with eventHandlersLock:
handlers = eventHandlers.copy()
eventHandlersLock.release()
if not event in handlers: return
for func in handlers[event]:
@ -73,10 +72,9 @@ def triggerEvent(event,*args,**kwargs):
return False
def addEventHandler(event,func):
eventHandlersLock.acquire()
with eventHandlersLock:
if not event in eventHandlers: eventHandlers[event] = []
eventHandlers[event].append(func)
eventHandlersLock.release()
def sendResponse(connection,data):
connection.sendall(len(data).to_bytes(4,"big") + data)
@ -94,8 +92,7 @@ class connectionThread(threading.Thread):
self.lock = threading.Lock()
def closeThread(self):
self.lock.acquire()
threadsLock.acquire()
with self.lock, threadsLock:
try:
self.connection.close()
except:
@ -105,14 +102,11 @@ class connectionThread(threading.Thread):
del threads[str(self.threadId)]
print("thread closed: " +str(self.threadId)+ " (open: " +str(len(threads))+ ")")
self.closed = True
threadsLock.release()
self.lock.release()
def run(self):
self.lock.acquire()
# inform about connection
with self.lock:
print("thread opened: " +", ".join((str(self.threadId),str(self.address))))
self.lock.release()
while True:
try:
@ -128,23 +122,18 @@ class connectionThread(threading.Thread):
# inform about request
cancel = triggerEvent("onPreRequest",self,requestLength)
self.lock.acquire()
with self.lock:
if self.closed:
self.lock.release()
return
self.lock.release()
if cancel: continue
# process request
cancel = triggerEvent("onRequest",self,requestLength)
self.lock.acquire()
with self.lock:
if self.closed:
self.lock.release()
return
self.lock.release()
if cancel: continue
except Exception as e:
#self.lock.release() - fix this
cancel = False
try:
cancel = triggerEvent("onException",self,e)
@ -190,10 +179,10 @@ def main():
global close
while True:
connection, address = socketServer.accept()
threadsLock.acquire()
if close: threadsLock.release(); break
with threadsLock:
if close: break
cancel = triggerEvent("onConnect",connection,address)
if close: threadsLock.release(); break
if close: break
if cancel: continue
threadId += 1
@ -203,7 +192,6 @@ def main():
thread = connectionThread(threadId,connection,address)
threads[str(threadId)] = thread
thread.start()
threadsLock.release()
if __name__ == '__main__':
main()

View File

@ -5,9 +5,8 @@ textCommandsLock = threading.Lock()
global textCommandRun
def textCommandRun(self,args):
textCommandsLock.acquire()
with textCommandsLock:
commands = textCommands.copy()
textCommandsLock.release()
if not args[0] in commands:
return ["error","nonfatal","command_not_found"]
@ -16,9 +15,8 @@ def textCommandRun(self,args):
global textCommandAddHandler
def textCommandAddHandler(command,function):
textCommandsLock.acquire()
with textCommandsLock:
textCommands[command] = function
textCommandsLock.release()
global textCommandToList
def textCommandToList(cmd):

View File

@ -28,10 +28,9 @@ def textUserRegister(self,command,args):
return ["error","nonfatal","invalid_name","Allowed characters: " +", ".join([char for char in textUserAllowedCharacters])]
userpath = textUserGetPath(user)
fileLock.acquire()
with fileLock:
if os.path.isdir(userpath):
fileLock.release()
return ["error","nonfatal","user_exists"]
password = args[1]
@ -40,7 +39,6 @@ def textUserRegister(self,command,args):
passFile = open(p(userpath,"pass.txt"),"w")
passFile.write(password)
passFile.close()
fileLock.release()
return ["ok"]
textCommandAddHandler("register",textUserRegister)
@ -55,14 +53,12 @@ def textUserLogin(self,command,args):
for symbol in user:
if not symbol in textUserAllowedCharacters:
fileLock.release()
return ["error","nonfatal","invalid_name","Allowed characters: " +", ".join([char for char in textUserAllowedCharacters])]
userpath = textUserGetPath(user)
fileLock.acquire()
with fileLock:
if not os.path.isdir(userpath):
fileLock.release()
return ["error","nonfatal","wrong_user_or_password"]
password = args[1]
@ -70,21 +66,20 @@ def textUserLogin(self,command,args):
passFile = open(p(userpath,"pass.txt"),"r")
passw = passFile.read()
passFile.close()
fileLock.release()
if password != passw:
return ["error","nonfatal","wrong_user_or_password"]
self.lock.acquire()
with self.lock:
self.user = user
self.lock.release()
return ["ok"]
textCommandAddHandler("login",textUserLogin)
global textUserGet
def textUserGet(self,command,args):
self.lock.acquire()
with self.lock:
user = self.user
self.lock.release()
if not user:
return ["error","nonfatal","not_logged_in"]

View File

@ -34,10 +34,9 @@ def textOnRequest(event,self,requestLength):
text = data.decode("utf-8")
print(":".join(map(str,self.address))+ " > " +text)
if text == "close":
threadsLock.acquire()
with threadsLock:
global close
close = True
threadsLock.release()
self.closeThread()
return True