Skip to content

Tracking Issue for MCP 1016: -Zasync-panic #163678

Closed as duplicate
Closed as duplicate

Description

@diondokter

This is a tracking issue for MCP "-Zasync-panic" (rust-lang/compiler-team#1016).

In short:
Add a flag to the compiler that allows users to turn off the panics in generated async code.
Instead of panicking, the code would return Poll::Pending. This saves on binary size (and possibly runtime performance).

About tracking issues

Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Discussion comments will get marked as off-topic or deleted.
Repeated discussions on the tracking issue may lead to the tracking issue getting locked.

Steps

Unresolved Questions

None so far

Implementation history

Empty so far

Activity

  1. added
    C-tracking-issue
    on Oct 2, 2026
  2. added
    I-lang-nominated
    on Oct 2, 2026
  3. GrigorenkoPV commented on Oct 4, 2026

    @GrigorenkoPV
    Contributor
  4. added
    I-lang-radar
    P-lang-drag-1
    on Oct 7, 2026
  5. scottmcm commented on Oct 7, 2026

    @scottmcm
    Member

    Having something here to improve the code size consequences does feel reasonable to me.

    I worry that Pending here is extremely misleading, though, since now there's a path where it returned pending without queueing a wakeup of some sort, which IIRC is 100% not supposed to happen.

    If we had an abort_noisily() to do an extremely small (int3 on x86 kinda thing) "this shouldn't have happened" maybe something like that could be more reasonable? Perhaps -Z async-panic=trap as an option?

    Do people looking at code size in this way use unwinding? Is the "panic" here actually typically people using the panic_immediate_abort cfg for code size in all panics?

  6. added
    T-lang
    on Oct 7, 2026
  7. yoshuawuyts commented on Oct 7, 2026

    @yoshuawuyts
    Member

    I worry that Pending here is extremely misleading, though, since now there's a path where it returned pending without queueing a wakeup of some sort, which IIRC is 100% not supposed to happen.

    @scottmcm That's right, the behavior proposed here is incompatible with the contract of Poll::Pending:

    [Pending] represents that a value is not ready yet. When a function returns Pending, the function must also ensure that the current task is scheduled to be awoken when progress can be made.

  8. diondokter commented on Oct 7, 2026

    @diondokter
    MemberAuthor

    That's true, though the future poll docs say:

    Once a future has completed (returned Ready from poll), calling its poll method again may panic, block forever, or cause other kinds of problems; the Future trait places no requirements on the effects of such a call.

  9. diondokter commented on Oct 7, 2026

    @diondokter
    MemberAuthor

    Do people looking at code size in this way use unwinding? Is the "panic" here actually typically people using the panic_immediate_abort cfg for code size in all panics?

    I wrote some things in the Zulip prior as to why I think adding this flag is still a good idea over just using panic-immediate-abort: https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/MCP.3A.20Add.20-Zasync-panic.20for.20binary.20size.20compiler-team.231016/near/628084205

  10. ZhekaS commented on Oct 7, 2026

    @ZhekaS

    Do people looking at code size in this way use unwinding? Is the "panic" here actually typically people using the panic_immediate_abort cfg for code size in all panics?

    I wrote some things in the Zulip prior as to why I think adding this flag is still a good idea over just using panic-immediate-abort: https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/MCP.3A.20Add.20-Zasync-panic.20for.20binary.20size.20compiler-team.231016/near/628084205

    I am one of the "people", and I am interested in using async in panic-free code, and filed this issue in the past.

  11. yoshuawuyts commented on Oct 7, 2026

    @yoshuawuyts
    Member

    9. That's true, though the future poll docs say:

    Once a future has completed (returned Ready from poll), calling its poll method again may panic, block forever, or cause other kinds of problems; the Future trait places no requirements on the effects of such a call.

    @diondokter You're right that the Future trait doesn't stipulate any restrictions, but the Poll::Pending variant does. The way I read the guarantee on Poll::Pending is as an attempt to define the necessary async liveness invariants. Scenarios like Barbara battles buffered streams exist because we failed to define all async liveness invariants. I don't think the way out of this though is to weaken the ones we did define.

    I think what we're working around here is a flaw in the design of futures: we have no way to represent a final "done" state, so the only reasonable alternative is to panic. I don't love that, as it means we're needing to choose between several options, all with their own downsides. Because the problem being presented here (async should not imply panic) is imo very reasonable.

  12. diondokter commented on Oct 7, 2026

    @diondokter
    MemberAuthor

    Well, but that's why this is presented as a flag and not a change forced on everyone.

    Panicking is reasonable! But for binary size it makes the wrong tradeoff. And for people writing panic free code it's also bad and a 'trap' panic or whatever you want to call it wouldn't solve the issue for them.

    So returning Pending serves two use cases and only costs us the one flag.

    The other approach that was originally brought up over a year ago was that a 'try_poll' function could be added to the Future trait. I think that could be made backwards compatible with a default impl. The generated futures would then return None there and the normal poll function would do the equivalent of self.try_poll(...).unwrap().

    Then no flag is required and nothing is breaking the Poll enum docs.

    But that approach has some downsides though. It makes the Future trait more complex and if the default impl is just Some(self.poll(...)), then it has to inherit the properties of the poll function where you shouldn't call it after Ready was returned. That's very confusing.

    Out of these two I think the flag will work best. I'd love it if there was a third way, but I don't think anybody has come up with anything

  13. ZhekaS commented on Oct 7, 2026

    @ZhekaS

    The other approach that was originally brought up over a year ago was that a 'try_poll' function could be added to the Future trait. I think that could be made backwards compatible with a default impl. The generated futures would then return None there and the normal poll function would do the equivalent of self.try_poll(...).unwrap().

    I believe from the user and backwards-compatibility perspective it is the best despite the downsides you mentioned.

  14. yoshuawuyts commented on Oct 7, 2026

    @yoshuawuyts
    Member

    Well, but that's why this is presented as a flag and not a change forced on everyone.

    @diondokter What I'm trying to communicate is that we have to change the guarantees on Poll::Pending for that flag to even be allowed to exist, even if it's not enabled by a user.

    Changing the signature of the poll function is a significant amount of work, though just like @ZhekaS I too would prefer it to a flag. Perhaps the way to move forward here is to introduce -Zasync-panic as you're proposing, but intentionally keeping it as an experiment only. That way we can gather data on the benefits of omitting panic paths, which can then be used to motivate making changes to the future trait.

  15. tmandry commented on Oct 7, 2026

    @tmandry
    Member

    Lang team talked about this in our meeting today. We have reservations about this being stabilized and would need further discussion before approving it (edit: After reviewing the thread above and prior art, I think this is unlikely to ever stabilize in its current form).

    However, we are fine with this moving forward as an experiment so we can gain more information, as @yoshuawuyts suggested in their last comment.

  16. RalfJung commented on Oct 9, 2026

    @RalfJung
    Member

    9. That's true, though the future poll docs say:
    > Once a future has completed (returned Ready from poll), calling its poll method again may panic, block forever, or cause other kinds of problems; the Future trait places no requirements on the effects of such a call.

    Yeah, agreed. To me this fairly clearly means that the normal promises attached to the return value do not hold if you call poll again after you got Ready. Otherwise, that comment you quote would be mostly meaningless.

    IOW, this is exactly like using a BTreeMap with an invalid PartialOrd. The usual type-level docs do not apply any more.

  17. removed
    I-lang-nominated
    P-lang-drag-1
    on Oct 13, 2026
  18. tmandry commented on Oct 13, 2026

    @tmandry
    Member

    Closing in favor of #164106 so we have only one tracking issue.

  19. oconnor663 commented on Oct 17, 2026

    @oconnor663
    Contributor

    When a function returns Pending, the function must also ensure that the current task is scheduled to be awoken when progress can be made.

    For what it's worth, I read this as saying that it's ok not to schedule any wakeups if in fact no progress can ever be made in the future. This is what std::future::pending does, so part of what we're asking here is whether that function is broken. (I don't think it is.) I also don't think there's much value in having a rule that says effectively "you have to schedule at least one wakeup", because the difference between pending() and sleep(A_MILLION_YEARS) is mostly academic.

    (Not sure this is important to the original proposal above, so commenting here instead of on the open issue.)

  20. yoshuawuyts commented on Oct 17, 2026

    @yoshuawuyts
    Member

    @oconnor663 I added that function to the stdlib while I was developing various async concurrency libraries a couple of years ago. It makes it easier to test operations involving multiple futures when you know for sure that one won't resolve, especially in no_std environments.

    I don't know of any legitimate uses core::future::pending could have outside of testing. It should probably come with a disclaimer: "intentionally broken, for testing purposes only". I wouldn't read too much into it.

  21. oconnor663 commented on Oct 18, 2026

    @oconnor663
    Contributor

    fwiw I've found it useful in fancy macros

  22. RalfJung commented on Oct 18, 2026

    @RalfJung
    Member

    I don't think it is a liveness requirement to not return Pending after Ready was returned. Even calling poll again in that situation is already a safety violation after all. What is a plausible example of a liveness problem this could introduce?

    The requirement for poll to set up a wakeup when returning Pending is a postcondition of poll. However, calling poll after Ready violates its precondition. When the precondition is violated, it is entirely in accordance with the spec to then violate the postcondition. Sometimes it's nice to satisfy the postcondition even after a precondition violation, but it's definitely not required, and in this case there apparently is a cost to doing so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueI-lang-radarT-lang

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions