From 1897a3038452f103b8beb2575af3fcf3dffec74b Mon Sep 17 00:00:00 2001 From: Gabriel Cruz Date: Mon, 8 Jun 2026 08:21:41 -0300 Subject: [PATCH] fix: last nits --- ffi/event_thread.nim | 3 ++- ffi/ffi_context.nim | 10 +++++----- ffi/ffi_events.nim | 8 +++++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/ffi/event_thread.nim b/ffi/event_thread.nim index 92caa56..81d9c77 100644 --- a/ffi/event_thread.nim +++ b/ffi/event_thread.nim @@ -47,7 +47,8 @@ proc emitLivenessEvent[T, P](ctx: ptr FFIContext[T], name: string, payload: P) = chronicles.error "liveness event encode failed", name = name, err = exc.msg return let dataPtr: pointer = - if event.len > 0: unsafeAddr event[0] else: nil + if event.len > 0: cast[pointer](unsafeAddr event[0]) + else: cast[pointer](emptyListenerPayload) ctx.dispatchToListeners(name, dataPtr, event.len) proc onNotResponding*(ctx: ptr FFIContext) = diff --git a/ffi/ffi_context.nim b/ffi/ffi_context.nim index 5d7afe6..705ad23 100644 --- a/ffi/ffi_context.nim +++ b/ffi/ffi_context.nim @@ -106,7 +106,7 @@ proc deinitContextResources*[T](ctx: ptr FFIContext[T]): Result[void, string] = if not ctx.eventThreadExitSignal.isNil(): ?ctx.eventThreadExitSignal.close() ctx.eventThreadExitSignal = nil - ok() + return ok() proc cleanUpResources[T](ctx: ptr FFIContext[T]): Result[void, string] = ## Full cleanup for heap-allocated contexts: closes all resources and frees memory. @@ -178,7 +178,7 @@ proc initContextResources*[T](ctx: ptr FFIContext[T]): Result[void, string] = return err("failed to create the event thread: " & getCurrentExceptionMsg()) success = true - ok() + return ok() proc signalStop*[T](ctx: ptr FFIContext[T]): Result[void, string] = # Error paths intentionally skip onNotResponding: a back-pressuring @@ -199,7 +199,7 @@ proc signalStop*[T](ctx: ptr FFIContext[T]): Result[void, string] = error "failed to signal eventQueueSignal in signalStop", error = evtSignaled.error elif evtSignaled.get() == false: error "failed to signal eventQueueSignal on time in signalStop" - ok() + return ok() ## If the FFI thread's event loop is blocked by a synchronous handler ## (e.g. blocking I/O), it cannot process reqSignal in time to exit. @@ -233,7 +233,7 @@ proc stopAndJoinThreads*[T](ctx: ptr FFIContext[T]): Result[void, string] = return err("event thread did not exit in time; leaking ctx to avoid hang") joinThread(ctx.eventThread) - ok() + return ok() proc clearContext[T](ctx: ptr FFIContext[T]): Result[void, string] = ## Stops the FFI context that was created via createFFIContext[T]() (heap). @@ -241,4 +241,4 @@ proc clearContext[T](ctx: ptr FFIContext[T]): Result[void, string] = return err("clearContext: " & $error) ctx.cleanUpResources().isOkOr: return err("cleanUpResources failed: " & $error) - ok() + return ok() diff --git a/ffi/ffi_events.nim b/ffi/ffi_events.nim index 9662154..29f8c89 100644 --- a/ffi/ffi_events.nim +++ b/ffi/ffi_events.nim @@ -203,7 +203,7 @@ proc eventQueueLen*(q: var EventQueue): int {.raises: [], gcsafe.} = return q.count -const emptyListenerPayload: cstring = "" +const emptyListenerPayload*: cstring = "" ## Non-nil zero-length buffer handed to listeners when a payload is ## empty, so a consumer doing `std::string(data, len)` / `memcpy` never ## receives a nil pointer (which is UB even at len 0). @@ -225,7 +225,8 @@ proc notifyListenersErr*(listeners: seq[FFIEventListener], msg: string) = ## Error fan-out: adapts the message string to `notifyListeners`, which ## supplies the non-nil pointer for the empty-message case. let p = - if msg.len > 0: cast[pointer](unsafeAddr msg[0]) else: nil + if msg.len > 0: cast[pointer](unsafeAddr msg[0]) + else: cast[pointer](emptyListenerPayload) notifyListeners(listeners, RET_ERR, p, msg.len) var ffiCurrentEventRegistry* {.threadvar.}: ptr FFIEventRegistry @@ -266,7 +267,8 @@ template dispatchFFIEvent*(eventName: string, body: untyped) = withFFIEventDispatch(eventName, listeners): let event = body let dataPtr: pointer = - if event.len > 0: unsafeAddr event[0] else: nil + if event.len > 0: cast[pointer](unsafeAddr event[0]) + else: cast[pointer](emptyListenerPayload) notifyListeners(listeners, RET_OK, dataPtr, event.len) template dispatchFFIEventCbor*(eventName: string, eventPayload: typed) =