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.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
try:
jsonSignal = statusSignal.parseJson
except CatchableError:
error "Invalid signal received", data = statusSignal
if allowLogging:
error "Invalid signal received", data = statusSignal
return
trace "Raw signal data", data = $jsonSignal
if allowLogging:
trace "Raw signal data", data = $jsonSignal
var signal:Signal
try:
signal = self.decode(jsonSignal)
except CatchableError:
warn "Error decoding signal", err=getCurrentExceptionMsg()
if allowLogging:
warn "Error decoding signal", err=getCurrentExceptionMsg()
return
if(signal.signalType == SignalType.NodeLogin):
if NodeSignal(signal).error != "":
if signal.signalType == SignalType.NodeLogin and NodeSignal(signal).error != "":
if allowLogging:
error "node.login", error=NodeSignal(signal).error
if(signal.signalType == SignalType.NodeCrashed):
if signal.signalType == SignalType.NodeCrashed:
if allowLogging:
error "node.crashed", error=statusSignal
self.events.emit(signal.signalType.event, signal)
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 =
let signalString = jsonSignal{"type"}.getStr

View File

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