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

[doc] poll_fn: explain how to pin captured state safely #109970

Merged

Conversation

danielhenrymantilla
Copy link
Contributor

Usage of Pin::new_unchecked(&mut …) is dangerous with poll_fn, even though the !Unpin-infectiousness has made things smoother. Nonetheless, there are easy ways to avoid the need for any unsafe altogether, be it through Box::pinning, or the pin! macro. Since the latter only works within an async context, showing an example artificially introducing one ought to help people navigate this subtlety with safety and confidence.

Preview

Screen.Recording.2023-04-05.at.15.18.03.mov

@rustbot label +A-docs

@rustbot
Copy link
Collaborator

rustbot commented Apr 5, 2023

r? @joshtriplett

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 5, 2023
@rustbot
Copy link
Collaborator

rustbot commented Apr 5, 2023

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rustbot rustbot added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Apr 5, 2023
@danielhenrymantilla
Copy link
Contributor Author

r? libs

@rustbot rustbot assigned cuviper and unassigned joshtriplett Apr 28, 2023
@cuviper
Copy link
Member

cuviper commented May 10, 2023

I don't feel comfortable reviewing pin-related things...

r? libs

@rustbot rustbot assigned thomcc and unassigned cuviper May 10, 2023
@danielhenrymantilla danielhenrymantilla force-pushed the add-poll-fn-pin-clarifications branch from 2fab966 to 6d1e3d7 Compare May 11, 2023 09:01
Comment on lines 44 to 45
/// // Remember: for a `Future` impl to be correct, any `Pending` return has to have,
/// // once ready, a matching `wake` call, lest it not be rescheduled for execution.
Copy link
Member

Choose a reason for hiding this comment

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

This is certainly necessary for this function to work as yield_once_then, but IIUC the explanation about yield_once_then is unrelated to the purpose of this section and distracting.

Is it possible to make the example simpler? Personally, I think an example that simply polls the future in a closure would be sufficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Regarding "distraction", I have now moved the detailed comment outside the code snippet, also deduplicating it.

We now have:

poll_fn(move |cx| if mem::take(&mut should_yield) {
    cx.waker().wake_by_ref();
    Poll::Pending
} else {
    pinned_fut.as_mut().poll(cx)
})

which is quite lightweight 🙂

Regarding simplicity, however, I disagree that this example should be further simplified, because at that point there is not that much incentive to use poll_fn to begin with, nor resemblance to actual async code. Documentation of practical functions such as this one are the best situation to educate people about not only the usefulness/handiness of poll_fn, but also of certain pitfalls when manually implementing poll, such as the Pending-without-wake mistake.

Copy link
Contributor

Choose a reason for hiding this comment

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

(Drive-by nitpicking; don't take this too seriously.)

Arguably, this example is already too simple — because there is an enclosing async block, and should_yield never becomes true after it is false, it is possible to rewrite

let mut pinned_fut = pin!(fut);
poll_fn(move |cx| if mem::take(&mut should_yield) {
    cx.waker().wake_by_ref();
    Poll::Pending
} else {
    pinned_fut.as_mut().poll(cx)
}).await

as

poll_fn(move |cx| if mem::take(&mut should_yield) {
    cx.waker().wake_by_ref();
    Poll::Pending
} else {
    Poll::Ready(())
}).await;
fut.await    // no need to pin!()

and this is (in my opinion) better-factored code, showing how to take maximum advantage of async {} rather than writing any more manual-polling code than you have to. I could worry that people learning async internals will read this code and wonder why not to write the latter.

My first idea for an example that actually needs the pin! and poll would be one which yields after a time interval has elapsed (adding yield points for better cooperative multitasking), but that's got perhaps a bit too much complexity.

Copy link
Contributor Author

@danielhenrymantilla danielhenrymantilla May 23, 2023

Choose a reason for hiding this comment

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

Yeah, that's a fair point; what about a naïve select, then?

That could be simple whilst legitimately polling the inner futures:

  /// Resolves to the first future that completes. In the event of a tie, `a` wins.
  fn naive_select<T>(
      a: impl Future<Output = T>,
      b: impl Future<Output = T>,
  ) -> impl Future<Output = T>
  {
+     async {
          let (mut a, mut b) =
-             (Box::pin(a), Box::pin(b))
+             (pin!(a), pin!(b))
          ;
          future::poll_fn(move |cx| {
              match (a.as_mut().poll(cx), b.as_mut().poll(cx)) {
                  (Poll::Ready(r), _) | (_, Poll::Ready(r)) => Poll::ready(r),
                  _ => Poll::Pending,
              }
          })
+         .await
+     }
  }

Copy link
Contributor

Choose a reason for hiding this comment

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

I like that example much better.

One further nitpick, though: it doesn't just let a win, it also polls b and discards the result if any, which good selects don't do and which will introduce cancellation bugs (e.g. if you are selecting among two message channels, this will drop some messages from the second). It's easy to fix at the cost of 3 more lines (but might be more readable anyway):

if let Poll::Ready(r) = a.as_mut().poll(cx) {
    Poll::Ready(r)
} else if let Poll::Ready(r) = b.as_mut().poll(cx) {
    Poll::Ready(r)
} else {
    Poll::Pending
}

(Thanks for taking my input. I'm bothering to write all this because in my experience, beginners will read examples and assume that everything in them is good practice even when it's not about the thing the example is for.)

Copy link
Contributor Author

@danielhenrymantilla danielhenrymantilla May 23, 2023

Choose a reason for hiding this comment

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

Started implementing this as of 9541287.

FWIW, this thus puts and end to the wake() noise/documentation question, since it's no longer relevant.

  • (I feel like a companion PR just implementing yield_once using poll_fn could be a nice thing to add nonetheless (but without pin! nor the _then part, to keep it simple), since I do still believe that the wake specificites ought to have examples. But I won't push for it myself, nor mention it anymore)

@thomcc
Copy link
Member

thomcc commented Jun 18, 2023

This was tough to review, but ultimately looks fine to me. Thanks a great deal to @kpreid and @taiki-e for their help with this review.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jun 18, 2023

📌 Commit 9541287e315d15cb851e58f598b39fc3cf597723 has been approved by thomcc

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 18, 2023
@danielhenrymantilla
Copy link
Contributor Author

danielhenrymantilla commented Jun 18, 2023

@thomcc now that you have reviewed all the discussion and so forth (thanks!), I'd like to squash the commits to keep the history (c)lean (since, according to rust-lang/bors#25, it is not possible to instruct bors to do it).

EDIT: Done (for those curious, old history)

Usage of `Pin::new_unchecked(&mut …)` is dangerous with `poll_fn`, even
though the `!Unpin`-infectiousness has made things smoother.
Nonetheless, there are easy ways to avoid the need for any `unsafe`
altogether, be it through `Box::pin`ning, or the `pin!` macro. Since the
latter only works within an `async` context, showing an example
artifically introducing one ought to help people navigate this subtlety
with safety and confidence.
@danielhenrymantilla danielhenrymantilla force-pushed the add-poll-fn-pin-clarifications branch from 9541287 to 94f7a79 Compare June 18, 2023 09:58
@thomcc
Copy link
Member

thomcc commented Jun 18, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Jun 18, 2023

📌 Commit 94f7a79 has been approved by thomcc

It is now in the queue for this repository.

jyn514 added a commit to jyn514/rust that referenced this pull request Jun 19, 2023
…n-clarifications, r=thomcc

[doc] `poll_fn`: explain how to `pin` captured state safely

Usage of `Pin::new_unchecked(&mut …)` is dangerous with `poll_fn`, even though the `!Unpin`-infectiousness has made things smoother. Nonetheless, there are easy ways to avoid the need for any `unsafe` altogether, be it through `Box::pin`ning, or the `pin!` macro. Since the latter only works within an `async` context, showing an example artificially introducing one ought to help people navigate this subtlety with safety and confidence.

## Preview

https://user-images.githubusercontent.com/9920355/230092494-da22fdcb-0b8f-4ff4-a2ac-aa7d9ead077a.mov

`@rustbot` label +A-docs
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 19, 2023
…n-clarifications, r=thomcc

[doc] `poll_fn`: explain how to `pin` captured state safely

Usage of `Pin::new_unchecked(&mut …)` is dangerous with `poll_fn`, even though the `!Unpin`-infectiousness has made things smoother. Nonetheless, there are easy ways to avoid the need for any `unsafe` altogether, be it through `Box::pin`ning, or the `pin!` macro. Since the latter only works within an `async` context, showing an example artificially introducing one ought to help people navigate this subtlety with safety and confidence.

## Preview

https://user-images.githubusercontent.com/9920355/230092494-da22fdcb-0b8f-4ff4-a2ac-aa7d9ead077a.mov

``@rustbot`` label +A-docs
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 19, 2023
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#109970 ([doc] `poll_fn`: explain how to `pin` captured state safely)
 - rust-lang#112705 (Simplify `Span::source_callee` impl)
 - rust-lang#112757 (Use BorrowFlag instead of explicit isize)
 - rust-lang#112768 (Rewrite various resolve/diagnostics errors as translatable diagnostics)
 - rust-lang#112777 (Continue folding in query normalizer on weak aliases)
 - rust-lang#112780 (Treat TAIT equation as always ambiguous in coherence)
 - rust-lang#112783 (Don't ICE on bound var in `reject_fn_ptr_impls`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 663fb5a into rust-lang:master Jun 19, 2023
@rustbot rustbot added this to the 1.72.0 milestone Jun 19, 2023
@danielhenrymantilla danielhenrymantilla deleted the add-poll-fn-pin-clarifications branch June 20, 2023 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants