C++ Logo

std-proposals

Advanced search

[std-proposals] Specify behavior of coroutine_handle::destroy

From: Nikl Kelbon <kelbonage_at_[hidden]>
Date: Wed, 21 Feb 2024 12:58:13 +0400
I will start with use-case:

Let we have recursive generator (C++23 have one...), imagine we call
.destroy on handle of one leaf of generator, then, top-level generator
destroyed, in dctor it calls .destroy, etc etc until last leaf destroyed,
but it has been already destroyed, undefined behavior.

What can implementation do?
Important note, we cannot just forbid this operation, in my particular use
case thread pool when destroyed calls .destroy on handles, because it is
only good way to not leak memory (coroutines suspended and waiting for
execution, thread pool destroyed, so they will just leak => thread poll
calls .destroy)

PROBLEM:

How coroutine frame can detect, that .destroy called and coroutine is not
just ended?
What .destroy do underspecified or i dont find this in standard

Good way - detect it in 'promise' destructor. If final_suspend was not
invoked and we are in promise::destructor, then, must be, .destroy on
handle invoked

But now there are no good way to do it

https://godbolt.org/z/7qzn6zG9x

PROPOSAL:
change wording so, that creating handle from promise in promise::destructor
is valid and specify, that .done() returns true if await_suspend was called
and false if not

Received on 2024-02-21 08:58:25