The ZFuture and Runnable traits need re-design #244
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR aims to reduce potentially blocking code in async context. I review the definition of
Runnable
andZFuture
traits and find something alarming to me.Runnable
trait binds arun()
method to a type.run()
is not async and can potentially block.ZFuture
adds await()
to a future type. It blocks on the wrapped future byasync_std::task::block_on(self.0)
.derive_zfuture!
macro implementsZFuture
andFuture
on a type withRunnable
. In the polling function, it callsself.run()
.The pitfall stems from step 3. Given that
run()
can potentially block, it can implicitly blocks the polling function. The developer could fall victim to it because it hides beneath thederive_zfuture!
macro.OTOH, the
future.wait()
should be used with caution. It could be accidentally called in async context and thus block the poll. The only relevant situations are calling async function indrop()
and exported C API. For several reasons, it is encouraged to let calling function to be async rather than thewait()
workaround. I would propose these changes.async fn build()
instead of being a future itself.Buidler::new().set_x(x).build().await
is more explicit thanBuidler::new().set_x(x).await
. It also removes the manualimpl Future on Builder {}
.wait()
across the zenoh source code exceptdrop()
. Make functions callingwait()
to be async.Runnable
trait. Move the code withinrun()
toFuture::poll()
or an async method of that type.wait()
to be private to public user because it must run in async-std context and user may not respect that.