Fixed potential logging deadlock

This commit is contained in:
Mark Qvist 2025-01-16 17:37:47 +01:00
parent b8aa6a3e44
commit a56d93fc1e

View File

@ -125,42 +125,36 @@ def log(msg, level=3, _override_destination = False, pt=False):
else: else:
logstring = "["+timestamp_str(time.time())+"] "+msg logstring = "["+timestamp_str(time.time())+"] "+msg
logging_lock.acquire() with logging_lock:
if (logdest == LOG_STDOUT or _always_override_destination or _override_destination):
print(logstring)
if (logdest == LOG_STDOUT or _always_override_destination or _override_destination): elif (logdest == LOG_FILE and logfile != None):
print(logstring) try:
logging_lock.release() file = open(logfile, "a")
file.write(logstring+"\n")
file.close()
if os.path.getsize(logfile) > LOG_MAXSIZE:
prevfile = logfile+".1"
if os.path.isfile(prevfile):
os.unlink(prevfile)
os.rename(logfile, prevfile)
elif (logdest == LOG_FILE and logfile != None): except Exception as e:
try: _always_override_destination = True
file = open(logfile, "a") log("Exception occurred while writing log message to log file: "+str(e), LOG_CRITICAL)
file.write(logstring+"\n") log("Dumping future log events to console!", LOG_CRITICAL)
file.close() log(msg, level)
if os.path.getsize(logfile) > LOG_MAXSIZE:
prevfile = logfile+".1"
if os.path.isfile(prevfile):
os.unlink(prevfile)
os.rename(logfile, prevfile)
logging_lock.release() elif logdest == LOG_CALLBACK:
except Exception as e: try:
logging_lock.release() logcall(logstring)
_always_override_destination = True except Exception as e:
log("Exception occurred while writing log message to log file: "+str(e), LOG_CRITICAL) _always_override_destination = True
log("Dumping future log events to console!", LOG_CRITICAL) log("Exception occurred while calling external log handler: "+str(e), LOG_CRITICAL)
log(msg, level) log("Dumping future log events to console!", LOG_CRITICAL)
log(msg, level)
elif logdest == LOG_CALLBACK:
try:
logcall(logstring)
logging_lock.release()
except Exception as e:
logging_lock.release()
_always_override_destination = True
log("Exception occurred while calling external log handler: "+str(e), LOG_CRITICAL)
log("Dumping future log events to console!", LOG_CRITICAL)
log(msg, level)
def rand(): def rand():