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

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

Merged
merged 2 commits into from
Jan 14, 2025
Merged

Conversation

richard-ramos
Copy link
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 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
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
Copy link
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 🙏

@richard-ramos richard-ramos merged commit 61ac0c5 into master Jan 14, 2025
18 checks passed
@richard-ramos richard-ramos deleted the async/pubsub branch January 14, 2025 16:01
@@ -162,7 +162,7 @@ type
.}
P2PPubSubCallback* = proc(
api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage
): Future[bool] {.gcsafe, raises: [CatchableError].}
): Future[bool] {.gcsafe, async: (raises: [CatchableError]).}
Copy link
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
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
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

@@ -271,7 +276,7 @@ proc connectOnce(p: PubSubPeer): Future[void] {.async.} =
finally:
await p.closeSendConn(PubSubPeerEventKind.StreamClosed)

proc connectImpl(p: PubSubPeer) {.async.} =
proc connectImpl(p: PubSubPeer) {.async: (raises: []).} =
Copy link
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
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

@@ -79,7 +81,7 @@ type
PubSubPeerEvent* = object
kind*: PubSubPeerEventKind

GetConn* = proc(): Future[Connection] {.gcsafe, raises: [].}
GetConn* = proc(): Future[Connection] {.gcsafe, async: (raises: [GetConnDialError]).}
Copy link
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

@@ -542,7 +555,7 @@ proc subscribe*(p: PubSub, topic: string, handler: TopicHandler) {.public.} =

method publish*(
p: PubSub, topic: string, data: seq[byte]
): Future[int] {.base, async, public.} =
): Future[int] {.base, async: (raises: [LPError]), public.} =
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: done
Development

Successfully merging this pull request may close these issues.

4 participants