fix: prevent recursive logging (#12916)

This commit is contained in:
Igor Sirotin 2023-12-04 17:11:54 +00:00 committed by GitHub
parent a18a94f5a9
commit 666c74dcc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 9 deletions

View File

@ -26,34 +26,44 @@ QtObject:
result.setup() result.setup()
result.events = events result.events = events
proc processSignal(self: SignalsManager, statusSignal: string) = # This method might be called with `ChroniclesLogs` event from `nim_status_client`.
# In such case we must not log anything to prevent recursive calls.
# I tried to make a better solution, but ended up with such workaround, check out PR for more details.
proc processSignal(self: SignalsManager, statusSignal: string, allowLogging: bool) =
var jsonSignal: JsonNode var jsonSignal: JsonNode
try: try:
jsonSignal = statusSignal.parseJson jsonSignal = statusSignal.parseJson
except CatchableError: except CatchableError:
error "Invalid signal received", data = statusSignal if allowLogging:
error "Invalid signal received", data = statusSignal
return return
trace "Raw signal data", data = $jsonSignal if allowLogging:
trace "Raw signal data", data = $jsonSignal
var signal:Signal var signal:Signal
try: try:
signal = self.decode(jsonSignal) signal = self.decode(jsonSignal)
except CatchableError: except CatchableError:
warn "Error decoding signal", err=getCurrentExceptionMsg() if allowLogging:
warn "Error decoding signal", err=getCurrentExceptionMsg()
return return
if(signal.signalType == SignalType.NodeLogin): if signal.signalType == SignalType.NodeLogin and NodeSignal(signal).error != "":
if NodeSignal(signal).error != "": if allowLogging:
error "node.login", error=NodeSignal(signal).error error "node.login", error=NodeSignal(signal).error
if(signal.signalType == SignalType.NodeCrashed): if signal.signalType == SignalType.NodeCrashed:
if allowLogging:
error "node.crashed", error=statusSignal error "node.crashed", error=statusSignal
self.events.emit(signal.signalType.event, signal) self.events.emit(signal.signalType.event, signal)
proc receiveSignal(self: SignalsManager, signal: string) {.slot.} = proc receiveSignal(self: SignalsManager, signal: string) {.slot.} =
self.processSignal(signal) self.processSignal(signal, allowLogging=true)
proc receiveChroniclesLogEvent(self: SignalsManager, signal: string) {.slot.} =
self.processSignal(signal, allowLogging=false)
proc decode(self: SignalsManager, jsonSignal: JsonNode): Signal = proc decode(self: SignalsManager, jsonSignal: JsonNode): Signal =
let signalString = jsonSignal{"type"}.getStr let signalString = jsonSignal{"type"}.getStr

View File

@ -51,7 +51,7 @@ proc prepareLogging() =
proc (logLevel: LogLevel, msg: LogOutputStr) {.gcsafe, raises: [Defect].} = proc (logLevel: LogLevel, msg: LogOutputStr) {.gcsafe, raises: [Defect].} =
try: try:
if signalsManagerQObjPointer != nil: if signalsManagerQObjPointer != nil:
signal_handler(signalsManagerQObjPointer, ($(%* {"type": "chronicles-log", "event": msg})).cstring, "receiveSignal") signal_handler(signalsManagerQObjPointer, ($(%* {"type": "chronicles-log", "event": msg})).cstring, "receiveChroniclesLogEvent")
except: except:
logLoggingFailure(cstring(msg), getCurrentException()) logLoggingFailure(cstring(msg), getCurrentException())