chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.93k stars 1.22k forks source link

Use-after-destroy of io_context::impl_ #1245

Closed ecatmur closed 1 year ago

ecatmur commented 1 year ago

We found the following msan error from some fairly involved tests:

==1==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x55d9f2a01c90 in long std::__1::__cxx_atomic_fetch_sub[abi:v160000]<long>(std::__1::__cxx_atomic_base_impl<long>*, long, std::__1::memory_order) /opt/clang-16.0.0rc2/bin/../include/c++/v1/atomic:1036:12
    #1 0x55d9f2a01c90 in std::__1::__atomic_base<long, true>::fetch_sub[abi:v160000](long, std::__1::memory_order) /opt/clang-16.0.0rc2/bin/../include/c++/v1/atomic:1655:17
    #2 0x55d9f2a01c90 in std::__1::__atomic_base<long, true>::operator--[abi:v160000]() /opt/clang-16.0.0rc2/bin/../include/c++/v1/atomic:1690:57
    #3 0x55d9f2a01c90 in boost::asio::detail::scheduler::work_finished() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/detail/scheduler.hpp:103:9
    #4 0x55d9f2a01c90 in boost::asio::io_context::basic_executor_type<std::__1::allocator<void>, 4ul>::~basic_executor_type() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/io_context.hpp:695:31
    #5 0x55d9f2a01c90 in void boost::asio::execution::detail::any_executor_base::destroy_object<boost::asio::io_context::basic_executor_type<std::__1::allocator<void>, 4ul>>(boost::asio::execution::detail::any_executor_base&) /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/execution/any_executor.hpp:766:23
    #6 0x55d9f31b7c5d in boost::asio::execution::detail::any_executor_base::~any_executor_base() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/execution/any_executor.hpp:536:5
    #7 0x55d9f31b7c5d in boost::asio::execution::any_executor<boost::asio::execution::context_as_t<boost::asio::execution_context&>, boost::asio::execution::detail::blocking::never_t<0>, boost::asio::execution::prefer_only<boost::asio::execution::detail::blocking::possibly_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::outstanding_work::tracked_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::outstanding_work::untracked_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::relationship::fork_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::relationship::continuation_t<0>>>::~any_executor() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/execution/any_executor.hpp:1380:7
    #8 0x55d9f31b7c5d in boost::optional_detail::optional_base<boost::asio::any_io_executor>::destroy_impl() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/optional/include/boost/optional/optional.hpp:779:49
    #9 0x55d9f31b7c5d in boost::optional_detail::optional_base<boost::asio::any_io_executor>::destroy() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/optional/include/boost/optional/optional.hpp:765:9
    #10 0x55d9f31b7c5d in boost::optional_detail::optional_base<boost::asio::any_io_executor>::~optional_base() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/optional/include/boost/optional/optional.hpp:263:24
    #11 0x55d9f31b7c5d in boost::beast::detail::execution_work_guard<boost::asio::any_io_executor>::~execution_work_guard() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/beast/include/boost/beast/core/detail/work_guard.hpp:35:8
    #12 0x55d9f31b7c5d in boost::beast::async_base<boost::beast::http::detail::write_op<boost::beast::http::detail::write_msg_op<boost::asio::executor_binder<void io::prometheus::MetricServerSession::processRequest<maven::concurrency::AsioSchedulerExecutor>(maven::concurrency::AsioSchedulerExecutor, std::__1::shared_ptr<io::prometheus::MetricServerSession>)::'lambda'(boost::system::error_code, unsigned long), maven::concurrency::AsioSchedulerExecutor>, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>, false, boost::beast::http::basic_dynamic_body<boost::beast::basic_multi_buffer<std::__1::allocator<char>>>, boost::beast::http::basic_fields<std::__1::allocator<char>>>, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>, boost::beast::http::detail::serializer_is_done, false, boost::beast::http::basic_dynamic_body<boost::beast::basic_multi_buffer<std::__1::allocator<char>>>, boost::beast::http::basic_fields<std::__1::allocator<char>>>, boost::asio::any_io_executor, std::__1::allocator<void>>::~async_base() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/beast/include/boost/beast/core/async_base.hpp:272:35
    #13 0x55d9f31c6abe in boost::beast::http::detail::write_some_op<boost::beast::http::detail::write_op<boost::beast::http::detail::write_msg_op<boost::asio::executor_binder<void io::prometheus::MetricServerSession::processRequest<maven::concurrency::AsioSchedulerExecutor>(maven::concurrency::AsioSchedulerExecutor, std::__1::shared_ptr<io::prometheus::MetricServerSession>)::'lambda'(boost::system::error_code, unsigned long), maven::concurrency::AsioSchedulerExecutor>, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>, false, boost::beast::http::basic_dynamic_body<boost::beast::basic_multi_buffer<std::__1::allocator<char>>>, boost::beast::http::basic_fields<std::__1::allocator<char>>>, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>, boost::beast::http::detail::serializer_is_done, false, boost::beast::http::basic_dynamic_body<boost::beast::basic_multi_buffer<std::__1::allocator<char>>>, boost::beast::http::basic_fields<std::__1::allocator<char>>>, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>, false, boost::beast::http::basic_dynamic_body<boost::beast::basic_multi_buffer<std::__1::allocator<char>>>, boost::beast::http::basic_fields<std::__1::allocator<char>>>::~write_some_op() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/beast/include/boost/beast/http/impl/write.hpp:37:7
    #14 0x55d9f31c6abe in boost::asio::detail::binder2<boost::beast::http::detail::write_some_op<boost::beast::http::detail::write_op<boost::beast::http::detail::write_msg_op<boost::asio::executor_binder<void io::prometheus::MetricServerSession::processRequest<maven::concurrency::AsioSchedulerExecutor>(maven::concurrency::AsioSchedulerExecutor, std::__1::shared_ptr<io::prometheus::MetricServerSession>)::'lambda'(boost::system::error_code, unsigned long), maven::concurrency::AsioSchedulerExecutor>, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>, false, boost::beast::http::basic_dynamic_body<boost::beast::basic_multi_buffer<std::__1::allocator<char>>>, boost::beast::http::basic_fields<std::__1::allocator<char>>>, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>, boost::beast::http::detail::serializer_is_done, false, boost::beast::http::basic_dynamic_body<boost::beast::basic_multi_buffer<std::__1::allocator<char>>>, boost::beast::http::basic_fields<std::__1::allocator<char>>>, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>, false, boost::beast::http::basic_dynamic_body<boost::beast::basic_multi_buffer<std::__1::allocator<char>>>, boost::beast::http::basic_fields<std::__1::allocator<char>>>, boost::system::error_code, unsigned long>::~binder2() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/detail/bind_handler.hpp:252:7
    #15 0x55d9f31c6abe in boost::asio::detail::reactive_socket_send_op<boost::beast::detail::buffers_ref<boost::beast::buffers_prefix_view<boost::beast::buffers_suffix<boost::beast::buffers_cat_view<boost::beast::detail::buffers_ref<boost::beast::buffers_cat_view<boost::asio::const_buffer, boost::asio::const_buffer, boost::asio::const_buffer, boost::beast::http::basic_fields<std::__1::allocator<char>>::writer::field_range, boost::beast::http::chunk_crlf>>, boost::beast::basic_multi_buffer<std::__1::allocator<char>>::subrange<false>>> const&>>, boost::beast::http::detail::write_some_op<boost::beast::http::detail::write_op<boost::beast::http::detail::write_msg_op<boost::asio::executor_binder<void io::prometheus::MetricServerSession::processRequest<maven::concurrency::AsioSchedulerExecutor>(maven::concurrency::AsioSchedulerExecutor, std::__1::shared_ptr<io::prometheus::MetricServerSession>)::'lambda'(boost::system::error_code, unsigned long), maven::concurrency::AsioSchedulerExecutor>, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>, false, boost::beast::http::basic_dynamic_body<boost::beast::basic_multi_buffer<std::__1::allocator<char>>>, boost::beast::http::basic_fields<std::__1::allocator<char>>>, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>, boost::beast::http::detail::serializer_is_done, false, boost::beast::http::basic_dynamic_body<boost::beast::basic_multi_buffer<std::__1::allocator<char>>>, boost::beast::http::basic_fields<std::__1::allocator<char>>>, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>, false, boost::beast::http::basic_dynamic_body<boost::beast::basic_multi_buffer<std::__1::allocator<char>>>, boost::beast::http::basic_fields<std::__1::allocator<char>>>, boost::asio::any_io_executor>::do_complete(void*, boost::asio::detail::scheduler_operation*, boost::system::error_code const&, unsigned long) /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/detail/reactive_socket_send_op.hpp:153:3
    #16 0x55d9f29f3747 in boost::asio::detail::scheduler_operation::destroy() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/detail/scheduler_operation.hpp:45:5
    #17 0x55d9f29f3747 in boost::asio::detail::scheduler::shutdown() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/detail/impl/scheduler.ipp:176:10
    #18 0x55d9f2b9e239 in boost::asio::detail::service_registry::shutdown_services() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/detail/impl/service_registry.ipp:44:14
    #19 0x55d9f2b9e239 in boost::asio::execution_context::shutdown() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/impl/execution_context.ipp:41:22
    #20 0x55d9f2b9e239 in boost::asio::execution_context::~execution_context() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/impl/execution_context.ipp:34:3
    #21 0x55d9f37d8e7c in boost::asio::io_context::~io_context() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/impl/io_context.ipp:58:1
    #22 0x55d9f37d8e7c in std::__1::default_delete<boost::asio::io_context>::operator()[abi:v160000](boost::asio::io_context*) const /opt/clang-16.0.0rc2/bin/../include/c++/v1/__memory/unique_ptr.h:65:5
    #23 0x55d9f37d8e7c in std::__1::unique_ptr<boost::asio::io_context, std::__1::default_delete<boost::asio::io_context>>::reset[abi:v160000](boost::asio::io_context*) /opt/clang-16.0.0rc2/bin/../include/c++/v1/__memory/unique_ptr.h:297:7
    #24 0x55d9f37d8e7c in maven::concurrency::AsioScheduler::~AsioScheduler() /home/edward.catmur/packages/maven.concurrency/msan/lib/src/asio_scheduler.cpp:27:16
    #25 0x55d9f3807d04 in maven::concurrency::ThreadContext::RunStateBase::~RunStateBase() /home/edward.catmur/packages/maven.concurrency/msan/lib/include/maven/concurrency/thread_context.hpp:60:34
    #26 0x55d9f3807d04 in maven::concurrency::ThreadContext::BlockRunState::~BlockRunState() /home/edward.catmur/packages/maven.concurrency/msan/lib/include/maven/concurrency/thread_context.hpp:73:12
    #27 0x55d9f3807d04 in maven::concurrency::ThreadContext::BlockRunState::~BlockRunState() /home/edward.catmur/packages/maven.concurrency/msan/lib/include/maven/concurrency/thread_context.hpp:73:12
    #28 0x55d9f38064f0 in std::__1::default_delete<maven::concurrency::ThreadContext::RunStateBase>::operator()[abi:v160000](maven::concurrency::ThreadContext::RunStateBase*) const /opt/clang-16.0.0rc2/bin/../include/c++/v1/__memory/unique_ptr.h:65:5
    #29 0x55d9f38064f0 in std::__1::unique_ptr<maven::concurrency::ThreadContext::RunStateBase, std::__1::default_delete<maven::concurrency::ThreadContext::RunStateBase>>::reset[abi:v160000](maven::concurrency::ThreadContext::RunStateBase*) /opt/clang-16.0.0rc2/bin/../include/c++/v1/__memory/unique_ptr.h:297:7
    #30 0x55d9f38064f0 in maven::concurrency::ThreadContext::~ThreadContext() /home/edward.catmur/packages/maven.concurrency/msan/lib/src/thread_context.cpp:134:15
    #31 0x55d9f2b4f57d in maven::saf::ThreadContext::~ThreadContext() /home/edward.catmur/packages/maven.saf/msan/lib/src/thread_context.cpp:438:1
    #32 0x55d9f298b911 in maven::saf::Application::runBackgroundThread(maven::saf::ThreadContext::Parameters const&, maven::logging::Severity, std::__1::promise<void>&, std::__1::promise<maven::concurrency::AsioScheduler*>&)::$_0::operator()() const /home/edward.catmur/packages/maven.saf/msan/lib/src/application.cpp:920:9
    #33 0x55d9f298b911 in std::__1::invoke_result<maven::saf::Application::runBackgroundThread(maven::saf::ThreadContext::Parameters const&, maven::logging::Severity, std::__1::promise<void>&, std::__1::promise<maven::concurrency::AsioScheduler*>&)::$_0>::type maven::saf::catchAnyException<maven::saf::Application::runBackgroundThread(maven::saf::ThreadContext::Parameters const&, maven::logging::Severity, std::__1::promise<void>&, std::__1::promise<maven::concurrency::AsioScheduler*>&)::$_0, maven::saf::Application::runBackgroundThread(maven::saf::ThreadContext::Parameters const&, maven::logging::Severity, std::__1::promise<void>&, std::__1::promise<maven::concurrency::AsioScheduler*>&)::$_1>(maven::saf::Application::runBackgroundThread(maven::saf::ThreadContext::Parameters const&, maven::logging::Severity, std::__1::promise<void>&, std::__1::promise<maven::concurrency::AsioScheduler*>&)::$_0&&, maven::saf::Application::runBackgroundThread(maven::saf::ThreadContext::Parameters const&, maven::logging::Severity, std::__1::promise<void>&, std::__1::promise<maven::concurrency::AsioScheduler*>&)::$_1&&) /home/edward.catmur/packages/maven.saf/msan/lib/include/maven/saf/any_exception.hpp:97:16
    #34 0x55d9f298b911 in maven::saf::Application::runBackgroundThread(maven::saf::ThreadContext::Parameters const&, maven::logging::Severity, std::__1::promise<void>&, std::__1::promise<maven::concurrency::AsioScheduler*>&) /home/edward.catmur/packages/maven.saf/msan/lib/src/application.cpp:791:5
    #35 0x55d9f2994ca8 in maven::saf::Application::Application(maven::saf::ApplicationParameters const&)::$_24::operator()() const /home/edward.catmur/packages/maven.saf/msan/lib/src/application.cpp:503:17
    #36 0x55d9f2994ca8 in decltype(std::declval<maven::saf::Application::Application(maven::saf::ApplicationParameters const&)::$_24>()()) std::__1::__invoke[abi:v160000]<maven::saf::Application::Application(maven::saf::ApplicationParameters const&)::$_24>(maven::saf::Application::Application(maven::saf::ApplicationParameters const&)::$_24&&) /opt/clang-16.0.0rc2/bin/../include/c++/v1/__functional/invoke.h:394:23
    #37 0x55d9f2994ca8 in void std::__1::__thread_execute[abi:v160000]<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct>>, maven::saf::Application::Application(maven::saf::ApplicationParameters const&)::$_24>(std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct>>, maven::saf::Application::Application(maven::saf::ApplicationParameters const&)::$_24>&, std::__1::__tuple_indices<>) /opt/clang-16.0.0rc2/bin/../include/c++/v1/thread:282:5
    #38 0x55d9f2994ca8 in void* std::__1::__thread_proxy[abi:v160000]<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct>>, maven::saf::Application::Application(maven::saf::ApplicationParameters const&)::$_24>>(void*) /opt/clang-16.0.0rc2/bin/../include/c++/v1/thread:293:5
    #39 0x7f9b7d3b8ea4 in start_thread pthread_create.c
    #40 0x7f9b7c9d3b0c in clone (/lib64/libc.so.6+0xfeb0c) (BuildId: 9470e279388f7f9cb2ed3b2872d0c2095b191ff4)

  Member fields were destroyed
    #0 0x55d9f26ccf4b in __sanitizer_dtor_callback_fields /clang-build-dir/llvm-project/compiler-rt/lib/msan/msan_interceptors.cpp:961
    #1 0x55d9f37d8e6b in boost::asio::io_context::~io_context() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/io_context.hpp:652:7
    #2 0x55d9f37d8e6b in boost::asio::io_context::~io_context() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/impl/io_context.ipp:58:1
    #3 0x55d9f37d8e6b in std::__1::default_delete<boost::asio::io_context>::operator()[abi:v160000](boost::asio::io_context*) const /opt/clang-16.0.0rc2/bin/../include/c++/v1/__memory/unique_ptr.h:65:5
    #4 0x55d9f37d8e6b in std::__1::unique_ptr<boost::asio::io_context, std::__1::default_delete<boost::asio::io_context>>::reset[abi:v160000](boost::asio::io_context*) /opt/clang-16.0.0rc2/bin/../include/c++/v1/__memory/unique_ptr.h:297:7
    #5 0x55d9f37d8e6b in maven::concurrency::AsioScheduler::~AsioScheduler() /home/edward.catmur/packages/maven.concurrency/msan/lib/src/asio_scheduler.cpp:27:16
    #6 0x55d9f3807d04 in maven::concurrency::ThreadContext::RunStateBase::~RunStateBase() /home/edward.catmur/packages/maven.concurrency/msan/lib/include/maven/concurrency/thread_context.hpp:60:34
    #7 0x55d9f3807d04 in maven::concurrency::ThreadContext::BlockRunState::~BlockRunState() /home/edward.catmur/packages/maven.concurrency/msan/lib/include/maven/concurrency/thread_context.hpp:73:12
    #8 0x55d9f3807d04 in maven::concurrency::ThreadContext::BlockRunState::~BlockRunState() /home/edward.catmur/packages/maven.concurrency/msan/lib/include/maven/concurrency/thread_context.hpp:73:12
    #9 0x55d9f38064f0 in std::__1::default_delete<maven::concurrency::ThreadContext::RunStateBase>::operator()[abi:v160000](maven::concurrency::ThreadContext::RunStateBase*) const /opt/clang-16.0.0rc2/bin/../include/c++/v1/__memory/unique_ptr.h:65:5
    #10 0x55d9f38064f0 in std::__1::unique_ptr<maven::concurrency::ThreadContext::RunStateBase, std::__1::default_delete<maven::concurrency::ThreadContext::RunStateBase>>::reset[abi:v160000](maven::concurrency::ThreadContext::RunStateBase*) /opt/clang-16.0.0rc2/bin/../include/c++/v1/__memory/unique_ptr.h:297:7
    #11 0x55d9f38064f0 in maven::concurrency::ThreadContext::~ThreadContext() /home/edward.catmur/packages/maven.concurrency/msan/lib/src/thread_context.cpp:134:15
    #12 0x55d9f2b4f57d in maven::saf::ThreadContext::~ThreadContext() /home/edward.catmur/packages/maven.saf/msan/lib/src/thread_context.cpp:438:1
    #13 0x55d9f298b911 in maven::saf::Application::runBackgroundThread(maven::saf::ThreadContext::Parameters const&, maven::logging::Severity, std::__1::promise<void>&, std::__1::promise<maven::concurrency::AsioScheduler*>&)::$_0::operator()() const /home/edward.catmur/packages/maven.saf/msan/lib/src/application.cpp:920:9
    #14 0x55d9f298b911 in std::__1::invoke_result<maven::saf::Application::runBackgroundThread(maven::saf::ThreadContext::Parameters const&, maven::logging::Severity, std::__1::promise<void>&, std::__1::promise<maven::concurrency::AsioScheduler*>&)::$_0>::type maven::saf::catchAnyException<maven::saf::Application::runBackgroundThread(maven::saf::ThreadContext::Parameters const&, maven::logging::Severity, std::__1::promise<void>&, std::__1::promise<maven::concurrency::AsioScheduler*>&)::$_0, maven::saf::Application::runBackgroundThread(maven::saf::ThreadContext::Parameters const&, maven::logging::Severity, std::__1::promise<void>&, std::__1::promise<maven::concurrency::AsioScheduler*>&)::$_1>(maven::saf::Application::runBackgroundThread(maven::saf::ThreadContext::Parameters const&, maven::logging::Severity, std::__1::promise<void>&, std::__1::promise<maven::concurrency::AsioScheduler*>&)::$_0&&, maven::saf::Application::runBackgroundThread(maven::saf::ThreadContext::Parameters const&, maven::logging::Severity, std::__1::promise<void>&, std::__1::promise<maven::concurrency::AsioScheduler*>&)::$_1&&) /home/edward.catmur/packages/maven.saf/msan/lib/include/maven/saf/any_exception.hpp:97:16
    #15 0x55d9f298b911 in maven::saf::Application::runBackgroundThread(maven::saf::ThreadContext::Parameters const&, maven::logging::Severity, std::__1::promise<void>&, std::__1::promise<maven::concurrency::AsioScheduler*>&) /home/edward.catmur/packages/maven.saf/msan/lib/src/application.cpp:791:5
    #16 0x55d9f2994ca8 in maven::saf::Application::Application(maven::saf::ApplicationParameters const&)::$_24::operator()() const /home/edward.catmur/packages/maven.saf/msan/lib/src/application.cpp:503:17
    #17 0x55d9f2994ca8 in decltype(std::declval<maven::saf::Application::Application(maven::saf::ApplicationParameters const&)::$_24>()()) std::__1::__invoke[abi:v160000]<maven::saf::Application::Application(maven::saf::ApplicationParameters const&)::$_24>(maven::saf::Application::Application(maven::saf::ApplicationParameters const&)::$_24&&) /opt/clang-16.0.0rc2/bin/../include/c++/v1/__functional/invoke.h:394:23
    #18 0x55d9f2994ca8 in void std::__1::__thread_execute[abi:v160000]<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct>>, maven::saf::Application::Application(maven::saf::ApplicationParameters const&)::$_24>(std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct>>, maven::saf::Application::Application(maven::saf::ApplicationParameters const&)::$_24>&, std::__1::__tuple_indices<>) /opt/clang-16.0.0rc2/bin/../include/c++/v1/thread:282:5
    #19 0x55d9f2994ca8 in void* std::__1::__thread_proxy[abi:v160000]<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct>>, maven::saf::Application::Application(maven::saf::ApplicationParameters const&)::$_24>>(void*) /opt/clang-16.0.0rc2/bin/../include/c++/v1/thread:293:5
    #20 0x7f9b7d3b8ea4 in start_thread pthread_create.c

SUMMARY: MemorySanitizer: use-of-uninitialized-value /opt/clang-16.0.0rc2/bin/../include/c++/v1/atomic:1036:12 in long std::__1::__cxx_atomic_fetch_sub[abi:v160000]<long>(std::__1::__cxx_atomic_base_impl<long>*, long, std::__1::memory_order)
Exiting

After a lot of head-scratching we realized that although the msan violation is reported in asio::detail::scheduler::work_finished(), it actually arises in asio::io_context::basic_executor_type<std::__1::allocator<void>, 4ul>::~basic_executor_type() - the bad read is actually the read of context_ptr()->impl_:

(gdb) frame
#5  boost::asio::io_context::basic_executor_type<std::__1::allocator<void>, 4ul>::~basic_executor_type (this=0x7ffff49f6378)
    at /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/io_context.hpp:695
695             context_ptr()->impl_->work_finished();
(gdb) call __msan_print_shadow((target_ & ~runtime_bits), 16)
Shadow map [0x22100000f000, 0x22100000f010) of [0x72100000f000, 0x72100000f010), 16 bytes:
0x22100000f000[0x72100000f000]: 00000000 00000000 ffffffff ffffffff  |. . A A|

Origin A (origin_id 2f65):
  Member fields were destroyed
    #0 0x555555ae2f4b in __sanitizer_dtor_callback_fields /clang-build-dir/llvm-project/compiler-rt/lib/msan/msan_interceptors.cpp:961
    #1 0x555556beee6b in boost::asio::io_context::~io_context() /home/edward.catmur/packages/boost/1.80.0-maven6/libs/asio/include/boost/asio/io_context.hpp:652:7
    (same trace)
ecatmur commented 1 year ago

Assuming beast isn't doing anything screwy here, I think it's valid for shutdown() to end up calling work_finished(). The problem is that shutdown() is called from the destructor ~execution_context(), when the derived class member io_context::impl_ has already been destroyed.

The simplest fix we've found is to add a call to shutdown() from ~io_context:


 io_context::~io_context()
 {
+  shutdown();
 }

 io_context::count_type io_context::run()
mabrarov commented 1 year ago

Hi @ecatmur,

With this change execution_context::service::shutdown method started being called twice for all services in destroyed instance of io_context. IMHO, it needs to be described in documentation of execution_context::service::shutdown method - the fact that this method can be called more than one time before instance of execution_context::service is destroyed. I got an issue in my custom Asio services after migration to Boost 1.82.0 and fixed it in mabrarov/asio_samples#206.

Thank you.

ecatmur commented 1 year ago

@mabrarov thanks, added to the documentation.

ecatmur commented 1 year ago

Fixed by https://github.com/chriskohlhoff/asio/commit/1408e2895c94c8e254e9e8ddd66ba083777f0dc2 (other than documentation).