Use with lock: instead of lock.acquire() and lock.release()
This commit is contained in:
parent
6591a01459
commit
7a1ec5d0ba
@ -61,9 +61,8 @@ def getModlist(path):
|
||||
return modList
|
||||
|
||||
def triggerEvent(event,*args,**kwargs):
|
||||
eventHandlersLock.acquire()
|
||||
handlers = eventHandlers.copy()
|
||||
eventHandlersLock.release()
|
||||
with eventHandlersLock:
|
||||
handlers = eventHandlers.copy()
|
||||
|
||||
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()
|
||||
if not event in eventHandlers: eventHandlers[event] = []
|
||||
eventHandlers[event].append(func)
|
||||
eventHandlersLock.release()
|
||||
with eventHandlersLock:
|
||||
if not event in eventHandlers: eventHandlers[event] = []
|
||||
eventHandlers[event].append(func)
|
||||
|
||||
def sendResponse(connection,data):
|
||||
connection.sendall(len(data).to_bytes(4,"big") + data)
|
||||
@ -94,25 +92,21 @@ class connectionThread(threading.Thread):
|
||||
self.lock = threading.Lock()
|
||||
|
||||
def closeThread(self):
|
||||
self.lock.acquire()
|
||||
threadsLock.acquire()
|
||||
try:
|
||||
self.connection.close()
|
||||
except:
|
||||
print("failed to close connection, ignoring.")
|
||||
pass
|
||||
|
||||
del threads[str(self.threadId)]
|
||||
print("thread closed: " +str(self.threadId)+ " (open: " +str(len(threads))+ ")")
|
||||
self.closed = True
|
||||
threadsLock.release()
|
||||
self.lock.release()
|
||||
with self.lock, threadsLock:
|
||||
try:
|
||||
self.connection.close()
|
||||
except:
|
||||
print("failed to close connection, ignoring.")
|
||||
pass
|
||||
|
||||
del threads[str(self.threadId)]
|
||||
print("thread closed: " +str(self.threadId)+ " (open: " +str(len(threads))+ ")")
|
||||
self.closed = True
|
||||
|
||||
def run(self):
|
||||
self.lock.acquire()
|
||||
# inform about connection
|
||||
print("thread opened: " +", ".join((str(self.threadId),str(self.address))))
|
||||
self.lock.release()
|
||||
with self.lock:
|
||||
print("thread opened: " +", ".join((str(self.threadId),str(self.address))))
|
||||
|
||||
while True:
|
||||
try:
|
||||
@ -128,23 +122,18 @@ class connectionThread(threading.Thread):
|
||||
|
||||
# inform about request
|
||||
cancel = triggerEvent("onPreRequest",self,requestLength)
|
||||
self.lock.acquire()
|
||||
if self.closed:
|
||||
self.lock.release()
|
||||
return
|
||||
self.lock.release()
|
||||
with self.lock:
|
||||
if self.closed:
|
||||
return
|
||||
if cancel: continue
|
||||
|
||||
# process request
|
||||
cancel = triggerEvent("onRequest",self,requestLength)
|
||||
self.lock.acquire()
|
||||
if self.closed:
|
||||
self.lock.release()
|
||||
return
|
||||
self.lock.release()
|
||||
with self.lock:
|
||||
if self.closed:
|
||||
return
|
||||
if cancel: continue
|
||||
except Exception as e:
|
||||
#self.lock.release() - fix this
|
||||
cancel = False
|
||||
try:
|
||||
cancel = triggerEvent("onException",self,e)
|
||||
@ -190,20 +179,19 @@ def main():
|
||||
global close
|
||||
while True:
|
||||
connection, address = socketServer.accept()
|
||||
threadsLock.acquire()
|
||||
if close: threadsLock.release(); break
|
||||
cancel = triggerEvent("onConnect",connection,address)
|
||||
if close: threadsLock.release(); break
|
||||
if cancel: continue
|
||||
|
||||
threadId += 1
|
||||
while str(threadId) in threads:
|
||||
with threadsLock:
|
||||
if close: break
|
||||
cancel = triggerEvent("onConnect",connection,address)
|
||||
if close: break
|
||||
if cancel: continue
|
||||
|
||||
threadId += 1
|
||||
|
||||
thread = connectionThread(threadId,connection,address)
|
||||
threads[str(threadId)] = thread
|
||||
thread.start()
|
||||
threadsLock.release()
|
||||
while str(threadId) in threads:
|
||||
threadId += 1
|
||||
|
||||
thread = connectionThread(threadId,connection,address)
|
||||
threads[str(threadId)] = thread
|
||||
thread.start()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -5,9 +5,8 @@ textCommandsLock = threading.Lock()
|
||||
|
||||
global textCommandRun
|
||||
def textCommandRun(self,args):
|
||||
textCommandsLock.acquire()
|
||||
commands = textCommands.copy()
|
||||
textCommandsLock.release()
|
||||
with textCommandsLock:
|
||||
commands = textCommands.copy()
|
||||
|
||||
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()
|
||||
textCommands[command] = function
|
||||
textCommandsLock.release()
|
||||
with textCommandsLock:
|
||||
textCommands[command] = function
|
||||
|
||||
global textCommandToList
|
||||
def textCommandToList(cmd):
|
||||
|
@ -28,20 +28,18 @@ def textUserRegister(self,command,args):
|
||||
return ["error","nonfatal","invalid_name","Allowed characters: " +", ".join([char for char in textUserAllowedCharacters])]
|
||||
|
||||
userpath = textUserGetPath(user)
|
||||
fileLock.acquire()
|
||||
|
||||
if os.path.isdir(userpath):
|
||||
fileLock.release()
|
||||
return ["error","nonfatal","user_exists"]
|
||||
|
||||
password = args[1]
|
||||
|
||||
os.makedirs(userpath)
|
||||
passFile = open(p(userpath,"pass.txt"),"w")
|
||||
passFile.write(password)
|
||||
passFile.close()
|
||||
fileLock.release()
|
||||
return ["ok"]
|
||||
with fileLock:
|
||||
if os.path.isdir(userpath):
|
||||
return ["error","nonfatal","user_exists"]
|
||||
|
||||
password = args[1]
|
||||
|
||||
os.makedirs(userpath)
|
||||
passFile = open(p(userpath,"pass.txt"),"w")
|
||||
passFile.write(password)
|
||||
passFile.close()
|
||||
return ["ok"]
|
||||
textCommandAddHandler("register",textUserRegister)
|
||||
|
||||
global textUserLogin
|
||||
@ -55,36 +53,33 @@ 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()
|
||||
|
||||
if not os.path.isdir(userpath):
|
||||
fileLock.release()
|
||||
return ["error","nonfatal","wrong_user_or_password"]
|
||||
with fileLock:
|
||||
if not os.path.isdir(userpath):
|
||||
return ["error","nonfatal","wrong_user_or_password"]
|
||||
|
||||
password = args[1]
|
||||
|
||||
passFile = open(p(userpath,"pass.txt"),"r")
|
||||
passw = passFile.read()
|
||||
passFile.close()
|
||||
if password != passw:
|
||||
return ["error","nonfatal","wrong_user_or_password"]
|
||||
|
||||
password = args[1]
|
||||
with self.lock:
|
||||
self.user = user
|
||||
|
||||
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()
|
||||
self.user = user
|
||||
self.lock.release()
|
||||
return ["ok"]
|
||||
textCommandAddHandler("login",textUserLogin)
|
||||
|
||||
global textUserGet
|
||||
def textUserGet(self,command,args):
|
||||
self.lock.acquire()
|
||||
user = self.user
|
||||
self.lock.release()
|
||||
with self.lock:
|
||||
user = self.user
|
||||
|
||||
if not user:
|
||||
return ["error","nonfatal","not_logged_in"]
|
||||
|
||||
|
@ -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()
|
||||
global close
|
||||
close = True
|
||||
threadsLock.release()
|
||||
with threadsLock:
|
||||
global close
|
||||
close = True
|
||||
|
||||
self.closeThread()
|
||||
return True
|
||||
|
Loading…
Reference in New Issue
Block a user