Boost C++ Libraries: Ticket #12898: Interprocess mutex: race condition crash (when using compiler optimization) https://svn.boost.org/trac10/ticket/12898 <p> Environment: </p> <pre class="wiki">OS: FreeBSD 11.0-RELEASE-p2 #0: Mon Oct 24 06:55:27 UTC 2016 root@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC amd64 Boost: boost-libs-1.63.0_1 Clang: FreeBSD clang version 3.8.0 (tags/RELEASE_380/final 262564) (based on LLVM 3.8.0) </pre><p> Description: program creates few named maps (boost::interprocess::map) in a shared memory (one map of custom structs and one map of sets (boost::interprocess::set)). Then multiple processes start to add elements into both maps concurrently. Each element has "time to live" value and "purge_expired" function is being called on each insertion. Named inter-process mutexes are being used for synchronizing access to each map. </p> <p> Steps to reproduce: </p> <pre class="wiki">1. Unpack attached archive to some folder (e.g. /tmp/testzero) 2. mkdir /tmp/testzero/build; cd /tmp/testzero/build; cmake ..; make 3. Run testzero (note: requires at least 4GB or RAM) </pre><p> Expected: program should pass all cycles and exit successfully <br /> Actual: program crashes after several seconds of execution (depends on CPU) When additional logging is enabled, we can often see two different processes pass "mutex lock" and changing same map simultaneously which leads to an exception. </p> <p> The issue is reproducible only when flags -O2 or -O3 are being used otherwise code works as expected. </p> <p> A stack dump: </p> <pre class="wiki">#0 boost::intrusive::rbtree_algorithms&lt;boost::intrusive::rbtree_node_traits&lt;boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, true&gt; &gt;::rebalance_after_insertion ( header=&lt;value optimized out&gt;, p=&lt;value optimized out&gt;) at offset_ptr.hpp:728 728 n.priv_offset() = tmp; [New Thread 801a16000 (LWP 100862/&lt;unknown&gt;)] (gdb) bt #0 boost::intrusive::rbtree_algorithms&lt;boost::intrusive::rbtree_node_traits&lt;boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, true&gt; &gt;::rebalance_after_insertion ( header=&lt;value optimized out&gt;, p=&lt;value optimized out&gt;) at offset_ptr.hpp:728 #1 0x0000000000404c62 in boost::interprocess::rbtree_best_fit&lt;boost::interprocess::mutex_family, boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, 0ul&gt;::priv_deallocate ( this=&lt;value optimized out&gt;, addr=&lt;value optimized out&gt;) at rbtree_algorithms.hpp:388 #2 0x00000000004045ad in boost::interprocess::rbtree_best_fit&lt;boost::interprocess::mutex_family, boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, 0ul&gt;::deallocate ( this=0x801c00010, addr=0x8028e32e1) at rbtree_best_fit.hpp:1321 #3 0x00000000004044df in boost::container::container_detail::private_node_pool_impl&lt;boost::interprocess::segment_manager_base&lt;boost::interprocess::rbtree_best_fit&lt;boost::interprocess::mutex_family, boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, 0ul&gt; &gt; &gt;::purge_blocks (this=0x8028e31f0) at segment_manager.hpp:227 #4 0x00000000004117b9 in boost::container::container_detail::node_alloc_holder&lt;boost::interprocess::private_node_allocator&lt;unsigned long, boost::interprocess::segment_manager&lt;char, boost::interprocess::rbtree_best_fit&lt;boost::interprocess::mutex_family, boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, 0ul&gt;, boost::interprocess::iset_index&gt;, 64ul&gt;, boost::intrusive::rbtree_impl&lt;boost::intrusive::bhtraits&lt;boost::container::container_detail::tree_node&lt;unsigned long, boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, (boost::container::tree_type_enum)0, true&gt;, boost::intrusive::rbtree_node_traits&lt;boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, true&gt;, (boost::intrusive::link_mode_type)0, boost::intrusive::dft_tag, 3u&gt;, void, boost::container::value_to_node_compare&lt;boost::container::container_detail::tree_node&lt;unsigned long, boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, (boost::container::tree_type_enum)0, true&gt;, boost::intrusive::tree_value_compare&lt;boost::interprocess::offset_ptr&lt;unsigned long, long, unsigned long, 0ul&gt;, std::__1::less&lt;unsigned long&gt;, boost::move_detail::identity&lt;unsigned long&gt;, true&gt; &gt;, unsigned long, true, void&gt; &gt;::~node_alloc_holder (this=0x8028e31f0) at node_pool_impl.hpp:88 #5 0x000000000041190f in boost::intrusive::bstree_impl&lt;boost::intrusive::bhtraits&lt;boost::container::container_detail::tree_node&lt;std::__1::pair&lt;unsigned long const, shared_memory::MappedValType&lt;boost::container::set&lt;unsigned long, std::__1::less&lt;unsigned long&gt;, boost::interprocess::private_node_allocator&lt;unsigned long, boost::interprocess::segment_manager&lt;char, boost::interprocess::rbtree_best_fit&lt;boost::interprocess::mutex_family, boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, 0ul&gt;, boost::interprocess::iset_index&gt;, 64ul&gt;, boost::container::tree_opt&lt;(boost::container::tree_type_enum)0, true&gt; &gt; &gt; &gt;, boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, (boost::container::tree_type_enum)0, true&gt;, boost::intrusive::rbtree_node_traits&lt;boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, true&gt;, (boost::intrusive::link_mode_type)0, boost::intrusive::dft_tag, 3u&gt;, void, boost::container::value_to_node_compare&lt;boost::container::container_detail::tree_node&lt;std::__1::pair&lt;unsigned long const, shared_memory::MappedValType&lt;boost::container::set&lt;unsigned long, std::__1::less&lt;unsigned long&gt;, boost::interprocess::private_node_allocator&lt;unsigned long, boost::interprocess::segment_manager&lt;char, boost::interprocess::rbtree_best_fit&lt;boost::interprocess::mutex_family, boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, 0ul&gt;, boost::interprocess::iset_index&gt;, 64ul&gt;, boost::container::tree_opt&lt;(boost::container::tree_type_enum)0, true&gt; &gt; &gt; &gt;, boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, (boost::container::tree_type_enum)0, true&gt;, boost::intrusive::tree_value_compare&lt;boost::interprocess::offset_ptr&lt;std::__1::pair&lt;unsigned long const, shared_memory::MappedValType&lt;boost::container::set&lt;unsigned long, std::__1::less&lt;unsigned long&gt;, boost::interprocess::private_node_allocator&lt;unsigned long, boost::interprocess::segment_manager&lt;char, boost::interprocess::rbtree_best_fit&lt;boost::interprocess::mutex_family, boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, 0ul&gt;, boost::interprocess::iset_index&gt;, 64ul&gt;, boost::container::tree_opt&lt;(boost::container::tree_type_enum)0, true&gt; &gt; &gt; &gt;, long, unsigned long, 0ul&gt;, std::__1::less&lt;unsigned long&gt;, boost::container::container_detail::select1st&lt;unsigned long&gt;, false&gt; &gt;, unsigned long, true, (boost::intrusive::algo_types)5, void&gt;::erase_and_dispose&lt;boost::container::container_detail::allocator_destroyer&lt;boost::interprocess::allocator&lt;boost::container::container_detail::tree_node&lt;std::__1::pair&lt;unsigned long const, shared_memory::MappedValType&lt;boost::container::set&lt;unsigned long, std::__1::less&lt;unsigned long&gt;, boost::interprocess::private_node_allocator&lt;unsigned long, boost::interprocess::segment_manager&lt;char, boost::interprocess::rbtree_best_fit&lt;boost::interprocess::mutex_family, boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, 0ul&gt;, boost::interprocess::iset_index&gt;, 64ul&gt;, boost::container::tree_opt&lt;(boost::container::tree_type_enum)0, true&gt; &gt; &gt; &gt;, boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, (boost::container::tree_type_enum)0, true&gt;, boost::interprocess::segment_manager&lt;char, boost::interprocess::rbtree_best_fit&lt;boost::interprocess::mutex_family, boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, 0ul&gt;, boost::interprocess::iset_index&gt; &gt; &gt; &gt; (this=&lt;value optimized out&gt;, i=&lt;value optimized out&gt;) at /tmp/testzero/main.cpp:64 #6 0x000000000040ee97 in shared_memory::Map&lt;unsigned long, boost::container::set&lt;unsigned long, std::__1::less&lt;unsigned long&gt;, boost::interprocess::private_node_allocator&lt;unsigned long, boost::interprocess::segment_manager&lt;char, boost::interprocess::rbtree_best_fit&lt;boost::interprocess::mutex_family, boost::interprocess::offset_ptr&lt;void, long, unsigned long, 0ul&gt;, 0ul&gt;, boost::interprocess::iset_index&gt;, 64ul&gt;, boost::container::tree_opt&lt;(boost::container::tree_type_enum)0, true&gt; &gt; &gt;::purge_expired (this=0x7fffffffea18, it=&lt;value optimized out&gt;, count=2) at tree.hpp:1101 #7 0x0000000000403a0d in shared_memory::MapSet&lt;unsigned long, unsigned long&gt;::add (this=0x7fffffffea18, k=@0x7fffffffe8c0, val=@0x7fffffffe8b8, expires=&lt;value optimized out&gt;) at /tmp/testzero/main.cpp:197 #8 0x0000000000402a4f in upd1 (map_foo=&lt;value optimized out&gt;, map_of_sets=@0x7fffffffea18, fstruct=@0x7fffffffe910) at /tmp/testzero/main.cpp:250 #9 0x0000000000402f0e in main (argc=&lt;value optimized out&gt;, argv=&lt;value optimized out&gt;) at /tmp/testzero/main.cpp:298 </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12898 Trac 1.4.3 Robert Ayrapetyan <robert.ayrapetyan@…> Sat, 11 Mar 2017 07:31:57 GMT attachment set https://svn.boost.org/trac10/ticket/12898 https://svn.boost.org/trac10/ticket/12898 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">testzero.7z</span> </li> </ul> Ticket robert.ayrapetyan@… Fri, 22 Jun 2018 01:11:52 GMT <link>https://svn.boost.org/trac10/ticket/12898#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12898#comment:1</guid> <description> <p> Cannot reproduce in latest FreeBSD (11.1-RELEASE-p10) with clang 4.0 and boost 1.66. Please close this issue. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Tue, 26 Jun 2018 21:31:10 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/12898#comment:2 https://svn.boost.org/trac10/ticket/12898#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Thanks for the report for both boost 1.63 and 1.66, closing. </p> Ticket