Skip to content

Commit

Permalink
Fix outstanding_work.tracked executor move assignment.
Browse files Browse the repository at this point in the history
Move assignments for io_context and thread pool executors are missing
the logic present in copy assignments that finishes work on old contexts.
This causes the following example to hang:

#include <boost/asio/execution/outstanding_work.hpp>
#include <boost/asio/prefer.hpp>
#include <boost/asio/static_thread_pool.hpp>

int main()
{
    asio::static_thread_pool tp1{1},tp2{1};
    {
        auto work = asio::prefer(tp1.get_executor(),
            asio::execution::outstanding_work.tracked);
        work = asio::prefer(tp2.get_executor(),
            asio::execution::outstanding_work.tracked);
    }
    tp1.join();
    tp2.join();
}
  • Loading branch information
palebedev authored and chriskohlhoff committed Jan 7, 2021
1 parent 5f39960 commit 00d6e4c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions asio/include/asio/impl/io_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,16 @@ io_context::basic_executor_type<Allocator, Bits>::operator=(
{
if (this != &other)
{
io_context* old_io_context = io_context_;
io_context_ = other.io_context_;
allocator_ = std::move(other.allocator_);
bits_ = other.bits_;
if (Bits & outstanding_work_tracked)
{
other.io_context_ = 0;
if (old_io_context)
old_io_context->impl_.work_finished();
}
}
return *this;
}
Expand Down
5 changes: 5 additions & 0 deletions asio/include/asio/impl/thread_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,16 @@ thread_pool::basic_executor_type<Allocator, Bits>::operator=(
{
if (this != &other)
{
thread_pool* old_thread_pool = pool_;
pool_ = other.pool_;
allocator_ = std::move(other.allocator_);
bits_ = other.bits_;
if (Bits & outstanding_work_tracked)
{
other.pool_ = 0;
if (old_thread_pool)
old_thread_pool->scheduler_.work_finished();
}
}
return *this;
}
Expand Down

0 comments on commit 00d6e4c

Please sign in to comment.