Skip to content

Commit

Permalink
fixed out of scope error for eager self-posts of generators.
Browse files Browse the repository at this point in the history
closes #142.
  • Loading branch information
klemens-morgenstern committed Dec 5, 2023
1 parent 7884039 commit 04af3e0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion include/boost/cobalt/detail/generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,16 @@ struct generator_receiver : generator_receiver_base<Yield, Push>
if (self->yield_from != nullptr && !self->lazy)
{
auto exec = self->yield_from->get_executor();
auto alloc = asio::get_associated_allocator(self->yield_from);
asio::post(
std::move(exec), std::exchange(self->yield_from, nullptr));
std::move(exec),
asio::bind_allocator(
alloc,
[y = std::exchange(self->yield_from, nullptr)]() mutable
{
if (y->receiver) // make sure we only resume eagerly when attached to a generator object
std::move(y)();
}));
}

return {system::in_place_value, self->get_result()};
Expand Down Expand Up @@ -492,6 +500,7 @@ struct generator_yield_awaitable

Push await_resume()
{
BOOST_ASSERT(self->receiver);
BOOST_ASSERT(self->receiver->pushed_value);
return *std::exchange(self->receiver->pushed_value, std::nullopt);
}
Expand Down
15 changes: 15 additions & 0 deletions test/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,19 @@ CO_TEST_CASE(detached_)
co_return;
}

cobalt::generator<int, int> detached_push()
{
int i = co_yield 42;
while (true)
i = co_yield i;
co_return i;
}

CO_TEST_CASE(detached_push_)
{
auto g = detached_push();

co_await g(1);
}

BOOST_AUTO_TEST_SUITE_END();

0 comments on commit 04af3e0

Please sign in to comment.