rust-lang / rust-lang/rust

Async function generates potentially panicking code

Open
#147,041 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-async-await C-discussion T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

It appears that async fn() {...} desugars into Future state machine with a possible panic!() path. I understand that this path is taken if poll() is executed on a finished task. This is a problem when trying to write a panic-free code (that is with no panic-related code linked to the final artifact).
I was not able to write a minimal reproducing example, so I will put here the excerpts from my full code (no_std, attempting to implement custom executor):

pub async fn async_number() -> u32 {
    42
}

pub async fn example_task() {
    let num = async_number().await;
    let _ = writeln!(dbg_print::DebugPrint, "Async number: {}", num);
}

This produces the following llvm-ir:

; test_futures::example_task::{{closure}}
; Function Attrs: inlinehint minsize nounwind optsize
define internal noundef zeroext i1 @"_ZN12test_futures12example_task28_$u7b$$u7b$closure$u7d$$u7d$17h69ea2006b28af955E"(ptr nocapture noundef nonnull align 1 %_1, ptr noalias nocapture readnone align 4 %_2) unnamed_addr #4 !dbg !1446 {
start:
  %_14 = alloca [0 x i8], align 1
  %args = alloca [8 x i8], align 4
  %_15 = alloca [24 x i8], align 4
  %num = alloca [4 x i8], align 4
  // ...................
  %0 = load i8, ptr %_1, align 1, !dbg !1477, !range !1478, !noundef !17
  switch i8 %0, label %bb7 [
    i8 0, label %bb4.thread
    i8 1, label %panic
    i8 3, label %bb4
  ], !dbg !1477

// ........

panic.i:                                          ; preds = %bb4
; call core::panicking::panic_const::panic_const_async_fn_resumed
  tail call fastcc void @_ZN4core9panicking11panic_const28panic_const_async_fn_resumed17h3b19b0997d8ed2ccE() #12, !dbg !1505
  unreachable, !dbg !1505

In the final disassembly the call to panic_const_async_fn_resumed() appears as well.

I am not sure what would be the right way to solve this. Perhaps introducing Future::try_poll() ? Or adding a "Finished" variant to the Poll enum.

UPDATE:
I believe the code responsible for generating the panic block is this:
https://github.com/rust-lang/rust/blob/a8858111044a9391ac7558f969d3bf62ef43222d/compiler/rustc_mir_transform/src/coroutine.rs#L1254-L1264

It is also documented:
https://github.com/rust-lang/rust/blob/a8858111044a9391ac7558f969d3bf62ef43222d/compiler/rustc_mir_transform/src/coroutine.rs#L43-L46

However it does not change the fact that async/await is not panic-free-friendly.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read compiler/rustc_mir_transform/src/coroutine.rs at lines 1254-1264 and the nearby documentation at lines 43-46, then compare the generated LLVM IR with the no_std example. A complete investigation should establish whether async lowering can meet panic-free requirements and define a regression check for the resulting artifact.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.