You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tracking Issue for MCP 1016: -Zasync-panic #163678
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-lang-radarItems that are on lang's radar and will need eventual work or consideration.T-langRelevant to the language team
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.
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?
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.
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.
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?
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?
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.
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
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.
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.
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.
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.
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.)
@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.
I don't think it is a liveness requirement to not return PendingafterReady 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.
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-lang-radarItems that are on lang's radar and will need eventual work or consideration.T-langRelevant to the language team
Activity
GrigorenkoPV commentedon Oct 4, 2026
Some prior art:
#[fused]#155733scottmcm commentedon Oct 7, 2026
Having something here to improve the code size consequences does feel reasonable to me.
I worry that
Pendinghere 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 (int3on x86 kinda thing) "this shouldn't have happened" maybe something like that could be more reasonable? Perhaps-Z async-panic=trapas an option?Do people looking at code size in this way use unwinding? Is the "panic" here actually typically people using the
panic_immediate_abortcfg for code size in all panics?yoshuawuyts commentedon Oct 7, 2026
@scottmcm That's right, the behavior proposed here is incompatible with the contract of
Poll::Pending:diondokter commentedon Oct 7, 2026
That's true, though the future poll docs say:
diondokter commentedon Oct 7, 2026
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/628084205ZhekaS commentedon Oct 7, 2026
I am one of the "people", and I am interested in using async in panic-free code, and filed this issue in the past.
yoshuawuyts commentedon Oct 7, 2026
@diondokter You're right that the
Futuretrait doesn't stipulate any restrictions, but thePoll::Pendingvariant does. The way I read the guarantee onPoll::Pendingis 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.
diondokter commentedon Oct 7, 2026
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
ZhekaS commentedon Oct 7, 2026
I believe from the user and backwards-compatibility perspective it is the best despite the downsides you mentioned.
yoshuawuyts commentedon Oct 7, 2026
@diondokter What I'm trying to communicate is that we have to change the guarantees on
Poll::Pendingfor that flag to even be allowed to exist, even if it's not enabled by a user.Changing the signature of the
pollfunction 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-panicas 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.tmandry commentedon Oct 7, 2026
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.
RalfJung commentedon Oct 9, 2026
Yeah, agreed. To me this fairly clearly means that the normal promises attached to the return value do not hold if you call
pollagain after you gotReady. Otherwise, that comment you quote would be mostly meaningless.IOW, this is exactly like using a
BTreeMapwith an invalidPartialOrd. The usual type-level docs do not apply any more.tmandry commentedon Oct 13, 2026
Closing in favor of #164106 so we have only one tracking issue.
oconnor663 commentedon Oct 17, 2026
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::pendingdoes, 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 betweenpending()andsleep(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.)
yoshuawuyts commentedon Oct 17, 2026
@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_stdenvironments.I don't know of any legitimate uses
core::future::pendingcould 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.oconnor663 commentedon Oct 18, 2026
fwiw I've found it useful in fancy macros
RalfJung commentedon Oct 18, 2026
I don't think it is a liveness requirement to not return
PendingafterReadywas returned. Even callingpollagain 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
pollto set up a wakeup when returningPendingis a postcondition ofpoll. However, callingpollafterReadyviolates 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.