Closed petrochenkov closed 8 months ago
This issue is not meant to be used for technical discussion. There is a Zulip stream for that. Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.
Concerns or objections to the proposal should be discussed on Zulip and formally registered here by adding a comment with the following syntax:
@rustbot concern reason-for-concern
<description of the concern>
Concerns can be lifted with:
@rustbot resolve reason-for-concern
See documentation at https://forge.rust-lang.org
cc @rust-lang/compiler @rust-lang/compiler-contributors
@rustbot second on the impl stuff
Should in parallel find a T-lang shepherd for the unstable (non-internal) lang features and lang items
@rustbot label -final-comment-period +major-change-accepted
Proposal
"Async drop" is one of the features on Async WG roadmap that is tentatively scheduled for 2024-2025. The feature would allow to perform future awaits (at higher level) aka coroutine suspensions (at lower level) in destructors when variables go out of scope, or in similar language constructions like
defer
/finally
blocks.The surface level, user-visible design for this feature is a contentious question (see the literature list below). The underlying mechanisms, however, are more or less shared between those surface designs.
More than that, majority of implementation work lies in those shared parts, like code generation. The work on the surface level features is more about selecting a specific design, and resolving backward compatibility and migration issues, than about implementation issues (with exception of borrow checking for defer/finally blocks, perhaps).
So, we suggest implementing those common components in rustc, and providing some minimal surface area to make them usable from the outside.
The components implemented as a result of this MCP must be sufficient for enabling implementation of a working and sound library for scoped tasks and/or structured concurrency, which is a major selling point for async drop related features.
Component 1: Async drop trait
The main trait in libcore providing the compiler with code to execute, and tying async drop to the type system from one side. The trait will be looking approximately like this, further details (what the
AsyncDropFuture
type is, in particular) are clarified by implementation.The goal is to support something that works and is functionally correct, but not necessarily optimally efficient. For example, additional futures may be created and support for types that want to keep the drop future state inline may be not provided, some type erasure or boxing may also be possible. A two-step protocol similar to
IntoFuture
could be later implemented to make drops more efficient (first convert originalT1
intoT2
that can be dropped using inline state, then async-dropT2
using apoll
-like interface,T1
may be the same asT2
and the conversion may be trivial).PRs in progress: https://github.com/rust-lang/rust/pull/121801
Component 2: Async drop glue
Implement automatic generation of async drop code for structures, enums, coroutines, and complex built-in types that either have their own
AsyncDrop
implementations, or have nested fields that need to be dropped synchronously or asynchronously."Manual" async dropping with
should work after this component is implemented.
PRs in progress: https://github.com/rust-lang/rust/pull/121801
Component 3: Calling async drop glue at the end of scope
Values of types implementing
AsyncDrop
or having nested fields implementing it should run the correspondingAsyncDrop
impls and drop glue and generateyield
s at the destruction points.These async drops are NOT yet tied to the type system from the opposite side than
AsyncDrop
trait, there are effectively noAsyncDrop
orSyncDrop
bounds. Async drops can be attempted in any code, including generic code, regardless of bounds, and will result in a post-monomorphization errors when called in inappropriate contexts.*Drop
bounds are an invasive change that we should be well motivated first (for example, by results of this experiment).Surface syntax for making async drops visible by humans (
let async
,await
blocks, etc) is NOT yet provided. It is useful for humans, but its design is contentious, and it is not strictly necessary for implementing a working scoped task / structured concurrency library.PRs in progress: https://github.com/rust-lang/rust/pull/123948
Component 4: Support for suspending tasks that are currently unwinding
We need to implement the "catch-suspend-resume-rethrow" protocol that would allow to catch a panic currently unwinding the stack, clear thread-local components of that panic so other tasks later running on the same thread don't see it as panicking, package the caught panic payload into a coroutine for suspension, and then unpackage and rethrow it after that coroutine's resumption.
Same or similar mechanism can work in both async destructors, or
defer
/finally
blocks to provide a reliable async cleanup on unwinding.Branches in progress: no work is happening right now
Component 5:
Forgettable
trait"Unforgettable" types are types that cannot be passed to
mem::forget
,Rc::new
and similar functions, and that are guaranteed to be either destroyed using (possibly async) drop or destructured, unlessunsafe
code is involved. The trait only really makes sense for non-'static
types, see more details in the proposal.Handles for scoped tasks are an example of such unforgettable types.
auto trait Forgettable {}
(could also be namedLeak
) is added to libcore, tying the unforgettable types to the type system from one side.Unforgettable types are NOT yet tied to the type system from the opposite side than
Forgettable
trait, there are effectively noForgettable
bounds.mem::forget
,Rc::new
and similar functions are marked with something like a#[rustc_forgettable]
attribute, and passing unforgettable types to them will result in post-monomorphization errors. Post-monomorphization checking will still accept code that needs to be accepted, and error on code that needs to be rejected, which is a minimum that is strictly necessary for implementing a working scoped task / structured concurrency library.Forgettable
bounds are an invasive change that we should be well motivated first (for example, by results of this experiment).Branches in progress: https://github.com/zetanumbers/rust/tree/postmono_forgettable Proposals: https://github.com/zetanumbers/posts/blob/main/myosotis.md
Component 6: Default bounds for potential new auto traits
We'd like to experiment with a mechanism for adding new auto traits like
Leak
, orSyncDrop
(or some others, specific traits don't matter here), to give a definitive answer to the question of whether we can do it in practice or not.For example, the
Leak
trait could be implicitly added to all bound lists on the current edition, and then added more conservatively and user-friendly using some heuristic on the next edition.There may be difficulties with backward compatibility due to cycles in trait solver, or with longer compilation times, or with comping up with a good heuristic for the next edition.
PRs in progress: https://github.com/rust-lang/rust/pull/120706, https://github.com/rust-lang/rust/pull/121676.
Literature
(Apologies to those not included, I know there is more.)
Async drop and async cancellation
Leak / Forgettable / linear types
Mentors or Reviewers
Design - @traviscross @yoshuawuyts, process - @petrochenkov, code review - some MIR/codegen experts are needed to review changes in MIR transforms/passes.
Process
The main points of the Major Change Process are as follows:
@rustbot second
.-C flag
, then full team check-off is required.@rfcbot fcp merge
on either the MCP or the PR.You can read more about Major Change Proposals on forge.
Comments
This issue is not meant to be used for technical discussion. There is a Zulip stream for that. Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.