Skip to content

feat(pubsub): add {.async: (raises).} annotations#1233

Merged
richard-ramos merged 2 commits intomasterfrom
async/pubsub
Jan 14, 2025
Merged

feat(pubsub): add {.async: (raises).} annotations#1233
richard-ramos merged 2 commits intomasterfrom
async/pubsub

Conversation

@richard-ramos
Copy link
Copy Markdown
Member

This PR adds {.async: (raises).} annotations to the pubsub package.
The cases in which a raises:[CatchableError] was added were due to not being part of the package and should probably be changed in a separate PR

Comment thread libp2p/protocols/pubsub/pubsub.nim Outdated
Comment on lines +615 to +617
except CatchableError as e:
trace "validator for message could not be executed, ignoring", topic = topic, err = e.msg
valResult = ValidationResult.Ignore
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid returning a CatchableError I capture the exception here, and ignore the message.

@richard-ramos richard-ramos marked this pull request as ready for review January 9, 2025 20:34
@richard-ramos richard-ramos force-pushed the async/pubsub branch 2 times, most recently from ad9c827 to e7f7642 Compare January 9, 2025 21:06
Comment thread tests/pubsub/testgossipsub2.nim Outdated
Comment thread tests/pubsub/testgossipsub2.nim Outdated
Comment thread examples/tutorial_4_gossipsub.nim Outdated
Comment thread libp2p/protocols/pubsub/gossipsub.nim
Comment thread libp2p/protocols/pubsub/pubsub.nim Outdated
Copy link
Copy Markdown
Collaborator

@kaiserd kaiserd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you 🙏

P2PPubSubCallback* = proc(
api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage
): Future[bool] {.gcsafe, raises: [CatchableError].}
): Future[bool] {.gcsafe, async: (raises: [CatchableError]).}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, to maintain "callback compatibility" in public api, what we do is to introduce a 2 overload, like so: https://github.com/status-im/nim-chronos/blob/7c5cbf04a6bb2258654f16cb1d51b4304986cfd1/chronos/transports/stream.nim#L121

then the overload using the old callback includes a "translation wrapper" and/or is deprecated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion! Will implement this in a subsequent PR

seen[peerName].inc
handler = proc(
topic: string, data: seq[byte]
) {.async: (raises: []), closure.} =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closure is a leftover from old nim versions, should not be needed any more

await p.closeSendConn(PubSubPeerEventKind.StreamClosed)

proc connectImpl(p: PubSubPeer) {.async.} =
proc connectImpl(p: PubSubPeer) {.async: (raises: []).} =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like it should propagate cancellederror - "most" api should do this to support cancellation - is there a specific reason here not to?

Copy link
Copy Markdown
Member Author

@richard-ramos richard-ramos Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. The prev version had an except CatchableError that captures the CancelledError and also interestingly enough a comment that says never cancelled

except CatchableError as exc: # never cancelled
debug "Could not establish send connection", description = exc.msg

Looking at the history, you introduced this change in 70deac9#diff-6f99d21f9b0d636313f9633209e5b125903440de8a134b40821e70c9b9ae4591 as it previously raised the CancelledError. Do you remember why?
image

kind*: PubSubPeerEventKind

GetConn* = proc(): Future[Connection] {.gcsafe, raises: [].}
GetConn* = proc(): Future[Connection] {.gcsafe, async: (raises: [GetConnDialError]).}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gcsafe is not needed either, with async

for some callbacks, it's worth considering allowing CancelledError - again, depending on the context, I don't remember this one in particular, but "cancelling a connection attempt" seems reasonable logically at least

method publish*(
p: PubSub, topic: string, data: seq[byte]
): Future[int] {.base, async, public.} =
): Future[int] {.base, async: (raises: [LPError]), public.} =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a function like publish should probably not be raising - ie it's a "fire-and-forget" kind of function that attempts to publish things to as many peers as possible but just like udp, there are no guarantees that it will reach anybody - as such, it doesn't really make sense that it raises, I think.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

publish can raise a LPError if anonimize is false and we enter this branch of code:

if sign:
raise (ref LPError)(msg: "Cannot sign message without peer info")

This can happen if passing a nil peerInfo when calling newSwitch, (although if SwitchBuilder is used, it will avoid that scenario, since build will populate the peerInfo anyway)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants