Skip to content

Commit

Permalink
Fix object lifetime issue when cancelling a co_spawn operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskohlhoff committed Jul 30, 2023
1 parent ba467b9 commit 22269a2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 2 additions & 0 deletions asio/include/asio/detail/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ namespace asio {
namespace detail {

#if defined(ASIO_HAS_STD_SHARED_PTR)
using std::allocate_shared;
using std::make_shared;
using std::shared_ptr;
using std::weak_ptr;
#else // defined(ASIO_HAS_STD_SHARED_PTR)
using boost::allocate_shared;
using boost::make_shared;
using boost::shared_ptr;
using boost::weak_ptr;
Expand Down
14 changes: 9 additions & 5 deletions asio/include/asio/impl/co_spawn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "asio/detail/config.hpp"
#include "asio/associated_cancellation_slot.hpp"
#include "asio/awaitable.hpp"
#include "asio/detail/memory.hpp"
#include "asio/detail/recycling_allocator.hpp"
#include "asio/dispatch.hpp"
#include "asio/execution/outstanding_work.hpp"
#include "asio/post.hpp"
Expand Down Expand Up @@ -229,27 +231,29 @@ class co_spawn_cancellation_handler
{
public:
co_spawn_cancellation_handler(const Handler&, const Executor& ex)
: ex_(ex)
: signal_(detail::allocate_shared<cancellation_signal>(
detail::recycling_allocator<cancellation_signal,
detail::thread_info_base::cancellation_signal_tag>())),
ex_(ex)
{
}

cancellation_slot slot()
{
return signal_.slot();
return signal_->slot();
}

void operator()(cancellation_type_t type)
{
cancellation_signal* sig = &signal_;
shared_ptr<cancellation_signal> sig = signal_;
asio::dispatch(ex_, [sig, type]{ sig->emit(type); });
}

private:
cancellation_signal signal_;
shared_ptr<cancellation_signal> signal_;
Executor ex_;
};


template <typename Handler, typename Executor>
class co_spawn_cancellation_handler<Handler, Executor,
typename enable_if<
Expand Down

0 comments on commit 22269a2

Please sign in to comment.