-
Notifications
You must be signed in to change notification settings - Fork 66
fix: create raceAndCancelPending proc #1175
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
base: master
Are you sure you want to change the base?
Conversation
a92c1bd to
e21b0e4
Compare
0f3b94b to
888385e
Compare
|
This looks useful, and perhaps should exist in chronos 🤔 |
| proc raceAndCancelPending*( | ||
| futs: seq[SomeFuture] | ||
| ): Future[void] {.async: (raises: [ValueError, CancelledError]).} = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
raceAndCancelPending should return future that finished the race, aka result from race, because one might need this future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should indeed, mostly because race is declared that way - that said, returning the future is actually of limited use since the value type is lost - most uses of race end up discarding it and using the "original" instance.
The other risk is that one forgets that more than one future might be finished by the time race resumes.
| let index = requests.find(raceFut) | ||
| requests.del(index) | ||
|
|
||
| proc raceAndCancelPending*( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about raceAndCancelPending -> raceAndCancel ?
seems better suited for cronos. @arnetheduck wdyt? |
| ## - `CancelledError` if the operation is canceled. | ||
| try: | ||
| discard await race(futs) | ||
| finally: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is no need for finally .. in particular, if race is cancelled, it should not cancel futs to maintain the race contract in general - ValueError means there are no futures, so nothing to cancel either
It looks useful, but the question is whether it is useful ;) It would be good to see a motivating example where it's actually put to use before adding to chronos proper - ie where in libp2p would you use it, for example? |
closest usecase: |
This is a classic example of the trap I mentioned - even if the handshake completes successfully by the time the race finishes, the code might prefer to treat it as a timeout which often isn't what you want - but more than that, it's worth rewriting the code to use |
The
raceproc in chronos doesn't cancel the remaining futures; sometimes that's what we need to do. This PR creates a proc that executes a race between a provided sequence of futures and cancels any remaining futures that have not yet been completed.Related to #1165 and #1170