Niko Matsakis
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # How much context for async error traces One of the challenges of the current async await design is that a requirement that a future must be `Send` often comes at some top-level function, where the actual problem that *prevents* the future from being `Send` occurs in some other function. We're struggling a bit at how much information to present and how in order to best explain what's going on. ## Simple example: one step Consider this example ([playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=026997547909cf9f9f68a0c38dc2dbaf)): ```rust fn is_send<T: Send>(t: T) { } fn main() { is_send(async_fn1(generate())); } async fn async_fn1(future: impl Future + Send) { let x = Mutex::new(22); let data = x.lock().unwrap(); future.await; } async fn generate() { } ``` Currently, it gives the following error: ``` error: future cannot be sent between threads safely --> src/main.rs:9:5 | 6 | fn is_send<T: Send>(t: T) { } | ------- ---- required by this bound in `is_send` ... 9 | is_send(async_fn1(generate())); | ^^^^^^^ future returned by `async_fn1` is not `Send` | = help: within `impl std::future::Future`, the trait `std::marker::Send` is not implemented for `std::sync::MutexGuard<'_, i32>` note: future is not `Send` as this value is used across an await --> src/main.rs:15:5 | 14 | let data = x.lock().unwrap(); | ---- has type `std::sync::MutexGuard<'_, i32>` 15 | future.await; | ^^^^^^^^^^^^ await occurs here, with `data` maybe used later 16 | } | - `data` is later dropped here ``` As the error explains, the problem is *reported* in the `main` function, but it's *caused by* the code in `async_fn1`. I think this error is pretty decent, though I'm definitely open to suggestions on how to improve this case. ## Example with multiple steps Now consider this case ([playground]()). The only difference is that `main` calls `async_fn3`, which in turn calls `async_fn2`, and ultimately `async_fn1`, which has the problem: ```rust fn is_send<T: Send>(t: T) { } fn main() { is_send(async_fn3(generate())); } async fn async_fn3(future: impl Future + Send) { async_fn2(future).await; } async fn async_fn2(future: impl Future + Send) { async_fn1(future).await; } async fn async_fn1(future: impl Future + Send) { let x = Mutex::new(22); let data = x.lock().unwrap(); future.await; } async fn generate() { } ``` The error we report in this case is largely unchanged: ``` error: future cannot be sent between threads safely --> src/main.rs:9:5 | 6 | fn is_send<T: Send>(t: T) { } | ------- ---- required by this bound in `is_send` ... 9 | is_send(async_fn3(generate())); | ^^^^^^^ future returned by `async_fn3` is not `Send` | = help: within `impl std::future::Future`, the trait `std::marker::Send` is not implemented for `std::sync::MutexGuard<'_, i32>` note: future is not `Send` as this value is used across an await --> src/main.rs:23:5 | 22 | let data = x.lock().unwrap(); | ---- has type `std::sync::MutexGuard<'_, i32>` 23 | future.await; | ^^^^^^^^^^^^ await occurs here, with `data` maybe used later 24 | } | - `data` is later dropped here ``` ## The concern There are two concerns with the error messages here: * They are complex and trying to pack in and explain a lot of stuff. * Also, with our current reporting, we show only the point of the error (`main`) and the leaf function (`async_fn1`) that caused the error. Users have to intuit the path between them. There is a bit of a trade-off here that we are trying to decide how to resolve. Adding more information makes the message more complex and foreboding, but leaving it out means that users have to figure out. ## How to resolve this? There are a few thoughts on how to resolve this. ### Do nothing We could keep the status quo. After all, if you want to fix the bug, almost always you will do so by altering the code in the leaf function, so it's not that interesting to find the path from the cause of error to the leaf function, and it's usually not that hard to find. @sfackler expressed this opinion in the past (not to put words in their mouth). ### Give the full stack trace At the other extreme, [PR #67116](https://github.com/rust-lang/rust/pull/67116/) proposed to alter our reporting in these "multi-step" to include the full details for each step along the way. So, for a [very similar example](https://github.com/rust-lang/rust/blob/25c30357d45435fae58ebcb50426682658617818/src/test/ui/async-await/nested-async-calls.rs), we get [output like this](https://raw.githubusercontent.com/rust-lang/rust/25c30357d45435fae58ebcb50426682658617818/src/test/ui/async-await/nested-async-calls.stderr): ``` error[E0277]: future cannot be sent between threads safely --> $DIR/nested-async-calls.rs:26:5 | LL | fn require_send<T: Send>(_val: T) {} | ------------ ---- required by this bound in `require_send` ... LL | require_send(wrapped); | ^^^^^^^^^^^^ future returned by `first` is not `Send` | = help: within `main::Wrapper<impl std::future::Future>`, the trait `std::marker::Send` is not implemented for `*const ()` = note: required because it appears within the type `third::{{closure}}#0::NotSend` note: future is not `Send` as this value is used across an await --> $DIR/nested-async-calls.rs:17:5 | LL | let _a: Outer; | -- has type `third::{{closure}}#0::Outer` LL | dummy().await; | ^^^^^^^^^^^^^ await occurs here, with `_a` maybe used later LL | } | - `_a` is later dropped here note: future is not `Send` as this value is used across an await --> $DIR/nested-async-calls.rs:8:5 | LL | third().await; | -------^^^^^^- `third()` is later dropped here | | | await occurs here, with `third()` maybe used later | has type `impl std::future::Future` note: future is not `Send` as this value is used across an await --> $DIR/nested-async-calls.rs:4:5 | LL | second().await; | --------^^^^^^- `second()` is later dropped here | | | await occurs here, with `second()` maybe used later | has type `impl std::future::Future` = note: required because it appears within the type `impl std::future::Future` = note: required because it appears within the type `main::Wrapper<impl std::future::Future>` error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. ``` ### Minimal notes A middle ground might be to note the functions in the stack trace, without giving full details (see the final "note" entries at the end), although we might want to show line numbers or some bit of more information as well: ``` error[E0277]: future cannot be sent between threads safely --> $DIR/nested-async-calls.rs:26:5 | LL | fn require_send<T: Send>(_val: T) {} | ------------ ---- required by this bound in `require_send` ... LL | require_send(wrapped); | ^^^^^^^^^^^^ future returned by `first` is not `Send` | = help: within `main::Wrapper<impl std::future::Future>`, the trait `std::marker::Send` is not implemented for `*const ()` = note: required because it appears within the type `third::{{closure}}#0::NotSend` note: future is not `Send` as this value is used across an await --> $DIR/nested-async-calls.rs:17:5 | LL | let _a: Outer; | -- has type `third::{{closure}}#0::Outer` LL | dummy().await; | ^^^^^^^^^^^^^ await occurs here, with `_a` maybe used later LL | } | - `_a` is later dropped here = note: `third()` is called from `second()` = note: `second()` is called from `first()` = note: `first()` is wrapped within the type within the type `main::Wrapper<impl std::future::Future>` error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. ``` ### Give user control We don't presently have an option to request verbose errors. We could add this. I am somewhat skeptical -- I think most folks won't know it exists, and I buy into the idea that we should try to tune the defaults to be as useful as we can (without being overwhelming) and avoid adding a lot of knobs. Knobs in particular feel like they will allow us to expend less effort on the defaults and -- since most folks won't use them -- the overall quality of our errors goes down. ## What I would like from you I'd like to know which of these options you prefer, of course, as well as other alterantives that we may not have considered. But I'm particularly interested in feedback from: * people who are using async-await frequently * specific examples of code where having the full backtrace would have been useful * or cases where you encountered the current (minimal) error and felt confused because it was hard to connect the two points of error * any suggestions on how we might improve the "core error" as well

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully