Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: changing libwaku's error handling format #3093

Merged
merged 2 commits into from
Oct 9, 2024

Conversation

gabrielmer
Copy link
Contributor

@gabrielmer gabrielmer commented Oct 7, 2024

Description

For some reason, when immediately chaining sendRequestToWakuThread's response with the handleRes template, there's cases in which the errors are not handled properly. See this message for a more detailed description of the issue.

It's probably due to some misusage of Nim templates, but until we further understand what's wrong with out chaining approach, modifying to a working version.

Update

The template usage was wrong - changed it to a proc and now works great :)

Changes

Issue

#3039

Copy link

github-actions bot commented Oct 7, 2024

You can find the image built from this PR at

quay.io/wakuorg/nwaku-pr:3093

Built from ad0c777

@gabrielmer
Copy link
Contributor Author

@arnetheduck whenever you're available could you please take a quick look? maybe you have a better understanding on what's going on 😶 we're not sure why does

waku_thread
  .sendRequestToWakuThread(
    ctx,
    RequestType.LIFECYCLE,
    NodeLifecycleRequest.createShared(NodeLifecycleMsgType.START_NODE),
  )
  .handleRes(callback, userData)

behave differently than

let res = waku_thread
  .sendRequestToWakuThread(
    ctx,
    RequestType.LIFECYCLE,
    NodeLifecycleRequest.createShared(NodeLifecycleMsgType.START_NODE),
  )

res.handleRes(callback, userData)

handleRes is defined here

template handleRes[T: string | void](
res: Result[T, string], callback: WakuCallBack, userData: pointer
) =
## Handles the Result responses, which can either be Result[string, string] or
## Result[void, string]. Notice that in case of Result[void, string], it is enough to
## just return RET_OK and not provide any additional feedback through the callback.
if res.isErr():
foreignThreadGc:
let msg = "libwaku error: " & $res.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
when T is string:
let msg = $res.get()
if msg.len > 0:
foreignThreadGc:
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK

@arnetheduck
Copy link
Contributor

[12:22]arnetheduck: the way the code is written, the handleRes template is calling sendRequestToWakuThread twice when there's an error, I'm pretty sure that's not what you intended
[12:24]arnetheduck: parameters in templates get their full ast expanded at every point of use - if you use res twice, you get two expansions of sendRequestToWakuThread...
[12:25]arnetheduck: that's why moving to let "fixes" it - instead of two calls, it accesses the variable twice

@gabrielmer gabrielmer force-pushed the fix-error-handling-in-libwaku branch from 4776eac to 73d0e41 Compare October 9, 2024 10:58
@gabrielmer gabrielmer merged commit 2e6c299 into master Oct 9, 2024
12 of 13 checks passed
@gabrielmer gabrielmer deleted the fix-error-handling-in-libwaku branch October 9, 2024 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants