Skip to content

Commit 730bd5f

Browse files
libwaku: better error handling and better waku thread destroy handling
1 parent 507b1fc commit 730bd5f

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

library/libwaku.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ proc waku_destroy(
168168
): cint {.dynlib, exportc.} =
169169
checkLibwakuParams(ctx, callback, userData)
170170

171-
waku_thread.stopWakuThread(ctx).handleRes(callback, userData)
171+
waku_thread.destroyWakuThread(ctx).handleRes(callback, userData)
172172

173173
proc waku_version(
174174
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer

library/waku_thread/waku_thread.nim

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,45 @@ type WakuContext* = object
1818
userData*: pointer
1919
eventCallback*: pointer
2020
eventUserdata*: pointer
21+
running: Atomic[bool] # To control when the thread is running
2122

2223
const git_version* {.strdefine.} = "n/a"
2324
const versionString = "version / git commit hash: " & waku.git_version
2425

25-
# To control when the thread is running
26-
# TODO: this should be part of the context so multiple instances can be executed
27-
var running: Atomic[bool]
28-
2926
proc runWaku(ctx: ptr WakuContext) {.async.} =
3027
## This is the worker body. This runs the Waku node
3128
## and attends library user requests (stop, connect_to, etc.)
32-
info "Starting Waku", version = versionString
3329

3430
var waku: Waku
3531

36-
while running.load == true:
32+
while true:
3733
await ctx.reqSignal.wait()
3834

39-
# Trying to get a request from the libwaku main thread
35+
if ctx.running.load == false:
36+
break;
37+
38+
## Trying to get a request from the libwaku requestor thread
4039
var request: ptr InterThreadRequest
4140
let recvOk = ctx.reqChannel.tryRecv(request)
42-
if recvOk == true:
43-
let resultResponse = waitFor InterThreadRequest.process(request, addr waku)
41+
if not recvOk:
42+
error "waku thread could not receive a request"
43+
continue
44+
45+
## Handle the request
46+
let resultResponse = waitFor InterThreadRequest.process(request, addr waku)
47+
48+
## Converting a `Result` into a thread-safe transferable response type
49+
let threadSafeResp = InterThreadResponse.createShared(resultResponse)
4450

45-
## Converting a `Result` into a thread-safe transferable response type
46-
let threadSafeResp = InterThreadResponse.createShared(resultResponse)
51+
## Send the response back to the thread that sent the request
52+
let sentOk = ctx.respChannel.trySend(threadSafeResp)
53+
if not sentOk:
54+
error "could not send a request to the requester thread", original_request = $request[]
4755

48-
## The error-handling is performed in the main thread
49-
discard ctx.respChannel.trySend(threadSafeResp)
50-
discard ctx.respSignal.fireSync()
56+
let fireRes = ctx.respSignal.fireSync()
57+
if fireRes.isErr():
58+
error "could not fireSync back to requester thread",
59+
original_request = $request[], error = fireRes.error
5160

5261
proc run(ctx: ptr WakuContext) {.thread.} =
5362
## Launch waku worker
@@ -62,7 +71,7 @@ proc createWakuThread*(): Result[ptr WakuContext, string] =
6271
ctx.respSignal = ThreadSignalPtr.new().valueOr:
6372
return err("couldn't create respSignal ThreadSignalPtr")
6473

65-
running.store(true)
74+
ctx.running.store(true)
6675

6776
try:
6877
createThread(ctx.thread, run, ctx)
@@ -74,15 +83,20 @@ proc createWakuThread*(): Result[ptr WakuContext, string] =
7483

7584
return ok(ctx)
7685

77-
proc stopWakuThread*(ctx: ptr WakuContext): Result[void, string] =
78-
running.store(false)
79-
let fireRes = ctx.reqSignal.fireSync()
80-
if fireRes.isErr():
81-
return err("error in stopWakuThread: " & $fireRes.error)
82-
discard ctx.reqSignal.close()
83-
discard ctx.respSignal.close()
86+
proc destroyWakuThread*(ctx: ptr WakuContext): Result[void, string] =
87+
ctx.running.store(false)
88+
89+
let signaledOnTime = ctx.reqSignal.fireSync().valueOr:
90+
return err("error in destroyWakuThread: " & $error)
91+
if not signaledOnTime:
92+
error "failed to signal reqSignal on time in destroyWakuThread"
93+
return err("failed to signal reqSignal on time in destroyWakuThread")
94+
8495
joinThread(ctx.thread)
96+
?ctx.reqSignal.close()
97+
?ctx.respSignal.close()
8598
freeShared(ctx)
99+
86100
return ok()
87101

88102
proc sendRequestToWakuThread*(

0 commit comments

Comments
 (0)