I have a boost::asio threadpool, which has a asio::io_service
and asio::work
:
boost::asio::io_service m_service;
boost::asio::io_service::work m_work;
In the threadpool destructor, I am calling m_service.stop()
and then joining all the threads. However running into this crash intermittently during process shutdown (in the threadpool destructor):
#0 0x0000000000000000 in ?? ()
#1 0x00007f4c8e59d160 in boost::asio::detail::task_io_service_operation::destroy (
this=0x6d579f8)
at /home/depot/Boost_1_61_0/boost/asio/detail/task_io_service_operation.hpp:43
#2 0x00007f4c8e59d115 in boost::asio::detail::op_queue_access::destroy (o=0x6d579f8)
at /home/depot/Boost_1_61_0/boost/asio/detail/op_queue.hpp:47
#3 0x00007f4c8e59d044 in boost::asio::detail::op_queue::~op_queue (this=0x6d57a20)
at /home/depot/Boost_1_61_0/boost/asio/detail/op_queue.hpp:81
#4 0x00007f4c8e685d65 in boost::asio::detail::task_io_service::~task_io_service (
this=0x6d57960)
at /home/depot/Boost_1_61_0/boost/asio/detail/task_io_service.hpp:40
#5 0x00007f4c8e685dc9 in boost::asio::detail::task_io_service::~task_io_service (
this=0x6d57960)
at /home/depot/Boost_1_61_0/boost/asio/detail/task_io_service.hpp:40
#6 0x00007f4c8e5aa1ce in boost::asio::detail::service_registry::destroy (
service=0x6d57960)
at /home/depot/Boost_1_61_0/boost/asio/detail/impl/service_registry.ipp:101
#7 0x00007f4c8e68537c in boost::asio::detail::service_registry::~service_registry (
this=0x6d55020)
at /home/depot/Boost_1_61_0/boost/asio/detail/impl/service_registry.ipp:45
#8 0x00007f4c8e6852b0 in boost::asio::io_service::~io_service (
this=0x7f4c8eb169c8 )
at /home/depot/Boost_1_61_0/boost/asio/impl/io_service.ipp:53
#9 0x00007f4c8e71d295 in crossplat::threadpool::~threadpool (
this=0x7f4c8eb169b0 )
at /home/depot/include/threadpool.h:87
#10 0x00007f4c8aed6fe8 in __run_exit_handlers (status=0,
---Type to continue, or q to quit---
listp=0x7f4c8b2605f8 , run_list_atexit=run_list_atexit@entry=true)
at exit.c:82
#11 0x00007f4c8aed7035 in __GI_exit (status=) at exit.c:104
#12 0x00007f4c8aebd837 in __libc_start_main (main=0x1ce0980 ,
argc=3, argv=0x7fffee2eda98, init=, fini=,
rtld_fini=, stack_end=0x7fffee2eda88) at ../csu/libc-start.c:325
#13 0x0000000001cdf159 in _start () at /usr/include/c++/v1/string:2027
The asio documentation suggests to explicitly destroy the asio::work
object for all handlers to finish normally. Questions:
- Is it required to call
io_service::stop()
after explicitly destructing the asio::work object? - What is the difference between
io_service::stop()
and destroyingio_service::work
- Does this threadpool destructor look right, can it help fix the above crash. I have changed m_work to a pointer
std::unique_ptr<boost::asio::io_service::work> m_work;
:
~threadpool()
{
m_work.reset(); // Allow io_service::run to exit
// join all threads
}