Boost C++ Libraries: Ticket #10213: Reproducible crash with Boost Asio and io_service::post https://svn.boost.org/trac10/ticket/10213 <p> On OSX, running the following code repeatedly results in an occasional crash with the stack trace following. Unfortunately, I can't find a workaround nor a fix since I don't actually understand why it is crashing... </p> <div class="wiki-code"><div class="code"><pre> <span class="k">using</span> <span class="k">namespace</span> <span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="p">;</span> <span class="n">io_service</span> <span class="n">service</span><span class="p">;</span> <span class="n">io_service</span><span class="o">::</span><span class="n">work</span> <span class="n">work</span><span class="p">(</span><span class="n">service</span><span class="p">);</span> <span class="n">boost</span><span class="o">::</span><span class="kr">thread</span> <span class="kr">thread</span><span class="p">{[</span><span class="o">&amp;</span><span class="p">](){</span> <span class="n">service</span><span class="p">.</span><span class="n">reset</span><span class="p">();</span> <span class="n">service</span><span class="p">.</span><span class="n">run</span><span class="p">();</span> <span class="p">}</span> <span class="p">};</span> <span class="n">service</span><span class="p">.</span><span class="n">post</span><span class="p">([</span><span class="o">&amp;</span><span class="n">service</span><span class="p">](){</span><span class="n">service</span><span class="p">.</span><span class="n">stop</span><span class="p">();});</span> <span class="kr">thread</span><span class="p">.</span><span class="n">join</span><span class="p">();</span> </pre></div></div><pre class="wiki">Error: signal 11: 0 KimiTestApp 0x0000000105534a02 _Z7handleri + 34 1 libsystem_platform.dylib 0x00007fff97a075aa _sigtramp + 26 2 KimiTestApp 0x00000001054f154d _ZN5boost6detail12shared_countD2Ev + 45 3 libsystem_pthread.dylib 0x00007fff9665691e _pthread_cond_signal + 612 4 KimiTestApp 0x0000000105642c43 _ZN5boost4asio6detail11posix_event17signal_and_unlockINS1_11scoped_lockINS1_11posix_mutexEEEEEvRT_ + 115 5 KimiTestApp 0x0000000105642ab4 _ZN5boost4asio6detail15task_io_service31wake_one_idle_thread_and_unlockERNS1_11scoped_lockINS1_11posix_mutexEEE + 100 6 KimiTestApp 0x000000010564293f _ZN5boost4asio6detail15task_io_service26wake_one_thread_and_unlockERNS1_11scoped_lockINS1_11posix_mutexEEE + 47 7 KimiTestApp 0x000000010564247e _ZN5boost4asio6detail15task_io_service25post_immediate_completionEPNS1_25task_io_service_operationEb + 206 8 KimiTestApp 0x000000010564002e _ZN5boost4asio6detail15task_io_service4postIZN42GlobalTestModule_TestNoOpDieImmediate_Test8TestBodyEvE3$_0EEvRT_ + 190 9 KimiTestApp 0x000000010563ff24 _ZN5boost4asio10io_service4postIZN42GlobalTestModule_TestNoOpDieImmediate_Test8TestBodyEvE3$_0EENS0_12async_resultINS0_12handler_typeIT_FvvEE4typeEE4typeEOS7_ + 68 10 KimiTestApp 0x000000010563fded _ZN42GlobalTestModule_TestNoOpDieImmediate_Test8TestBodyEv + 125 </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/10213 Trac 1.4.3 boost-bugs@… Fri, 18 Jul 2014 11:23:06 GMT severity changed https://svn.boost.org/trac10/ticket/10213#comment:1 https://svn.boost.org/trac10/ticket/10213#comment:1 <ul> <li><strong>severity</strong> <span class="trac-field-old">Showstopper</span> → <span class="trac-field-new">Problem</span> </li> </ul> Ticket Ben Strong <bstrong@…> Tue, 22 Jul 2014 21:09:12 GMT <link>https://svn.boost.org/trac10/ticket/10213#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10213#comment:2</guid> <description> <p> We hit this bug and fixed it with the following patch: </p> <pre class="wiki">--- a/boost/asio/detail/posix_event.hpp +++ b/boost/asio/detail/posix_event.hpp @@ -58,8 +58,8 @@ public: { BOOST_ASIO_ASSERT(lock.locked()); signalled_ = true; - lock.unlock(); ::pthread_cond_signal(&amp;cond_); // Ignore EINVAL. + lock.unlock(); } // Reset the event. </pre><p> The issue is that in the task_io_service, the thread_info contains an event, and the thread_info lifetime is protected by the mutex associated with the lock passed into <code>signal_and_unlock</code>, so there is a race where the event is destroyed after the <code>lock.unlock()</code> but before the <code>pthread_cond_signal()</code>. </p> <p> The "Ignore EINVAL" comment suggests that someone hit this issue before. Unfortunately, relying on pthread_cond_signal to return an error (and not crash) is not a sufficient solution. </p> </description> <category>Ticket</category> </item> <item> <author>boost-bugs@…</author> <pubDate>Wed, 23 Jul 2014 13:38:02 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10213#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10213#comment:3</guid> <description> <p> I can confirm that the crash is gone with this change, thanks for posting. </p> </description> <category>Ticket</category> </item> <item> <author>ikostov@…</author> <pubDate>Thu, 21 Sep 2017 11:18:54 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10213#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10213#comment:4</guid> <description> <p> found similar issues <a class="ext-link" href="https://svn.boost.org/trac10/ticket/12690"><span class="icon">​</span>https://svn.boost.org/trac10/ticket/12690</a>. Patch works for me as well </p> </description> <category>Ticket</category> </item> </channel> </rss>