This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of New status.
task's coroutine frame may be released lateSection: 33.13.6 [exec.task] Status: New Submitter: Dietmar Kühl Opened: 2025-08-31 Last modified: 2025-10-27
Priority: 2
View all issues with New status.
Discussion:
The specification of task doesn't spell out when the
coroutine frame is destroyed (i.e., when handle.destroy()
is called). As a result the lifetime of arguments passed to the
coroutine and/or of local variables in the coroutine body may be
longer than expected.
The intention is that the coroutine frame is destroyed before any
of the completion functions is called. One implication of this
requirement is that the result and error objects can't be stored
in the promise_type when the completion function is
called although the exposition-only members result
and errors imply exactly that. Instead the data
needs to be stored in the operation state object or it needs to be
moved to a different place before calling destroy().
The proposed resolution is to add a paragraph to the specification
of promise_type in 33.13.6.5 [task.promise] that spells
out that the coroutine frame is destroyed before any of the completion
functions is called. To actually do that the exposition-only members
result and errors can't
remain as members of the promise_type. While writing
the relevant change it turned out that errors
is a variant which only ever stores an
exception_ptr (the other potential errors are immediately
reported via the awaiter return from yield_value).
Thus, the variant can be replaced with an
exception_ptr.
[2025-10-17; Reflector poll.]
Set priority to 2 after reflector poll.
"P/R is incomplete - also needs to update at least promise_type::uncaught_exception()."
[2025-10-24; Resolves US 242-372]
Proposed resolution:
In 33.13.6.4 [task.state], add
exposition-only data members result and
error to the exposition-only class
state:
namespace std::execution {
template<class T, class Environment>
template<receiver Rcvr>
class task<T, Environment>::state { // exposition only
...
Environment environment; // exposition only
optional<T> result; // exposition only; present only if is_void_v<T> is false
exception_ptr error; // exposition-only
};
}
Remove the exposition-only data members
result and errors from
the class promise_type in
33.13.6.5 [task.promise]:
namespace std::execution {
template<class T, class Environment>
class task<T, Environment>::promise_type {
...
stop_token_type token; // exposition only
optional<T> result; // exposition only; present only if is_void_v<T> is false
error-variant errors; // exposition only
};
}
The definition of error-variant isn't needed, i.e., remove 33.13.6.5 [task.promise] paragraph 2:
-2-error-variantis avariant<monostate, remove_cvref_t<E>...>, with duplicate types removed, whereE...are template arguments of the specialization ofexecution::completion_signaturesdenoted byerror_types.
In 33.13.6.5 [task.promise] change paragraph 7 to use the members added to state:
auto final_suspend() noexcept-7- Returns: An awaitable object of unspecified type (7.6.2.4 [expr.await]) whose member functions arrange for the completion of the asynchronous operation associated with
STATE(*this)by invoking. Letstbe a reference toSTATE(*this). The asynchronous completion first destroys the coroutine frame usingst.handle.destroy()and then invokes:
- -7.1-
set_error(std::move(ifRCVR(*this)st.rcvr), std::move(est.error))errors.index()is greater than zero andeis the value held byerrorsbool(st.error)istrue, otherwise- -7.2-
set_value(std::move(ifRCVR(*this)st.rcvr))is_void<T>istrue, and otherwise- -7.3-
set_value(std::move(.RCVR(*this)st.rcvr), *st.result)
Change the specification of yield_value to destroy the coroutine frame before invoking the set_error completion, i.e., change 33.13.6.5 [task.promise] paragraph 9:
-9- Returns: An awaitable object of unspecified type ([expr.await]) whose member functions arrange for the calling coroutine to be suspended and then completes the asynchronous operation associated with
STATE(*this)by. Letstbe a reference toSTATE(*this). Then the asynchronous operation completes by first destroying the coroutine frame usingst.handle.destroy()and then invokingset_error(std::move(.RCVR(*this)st.rcvr), Cerr(std::move(err.error)))
Change the specification of unhandled_stopped to destroy the coroutine frame before invoking the set_stopped completion, i.e., change 33.13.6.5 [task.promise] paragraph 13:
coroutine_handle<> unhandled_stopped();-13- Effects: Completes the asynchronous operation associated with
STATE(*this)by. Letstbe a reference toSTATE(*this). The asynchronous operation is completed by first destroying the coroutine frame usingst.handle.destroy()and then invokingset_stopped(std::move(.RCVR(*this)st.rcvr))