Boost C++ Libraries: Ticket #13294: Boost(1.57).Asio crashes after ssl stream is closed and stream is deleted. https://svn.boost.org/trac10/ticket/13294 <p> The issue is found in boost asio v1.57 and is presents in 1.65.1. Windows, Visual Studio 2015 </p> <p> Our library uses boost asio ssl stream for secure TCP connection. We use dynamically created context and ssl stream: </p> <div class="wiki-code"><div class="code"><pre><span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">ssl_context</span><span class="o">&gt;</span> <span class="n">m_context</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">ssl_stream</span><span class="o">&gt;</span> <span class="n">m_stream</span><span class="p">;</span> <span class="p">...</span> <span class="n">m_context</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">make_shared</span><span class="o">&lt;</span><span class="n">ssl_context</span><span class="o">&gt;</span><span class="p">(</span><span class="n">TM_NS_ASIO</span><span class="o">::</span><span class="n">ssl</span><span class="o">::</span><span class="n">context</span><span class="o">::</span><span class="n">tlsv12</span><span class="p">);</span> <span class="n">m_stream</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">make_shared</span><span class="o">&lt;</span><span class="n">ssl_stream</span><span class="o">&gt;</span><span class="p">(</span><span class="o">*</span><span class="n">io_service</span><span class="p">,</span> <span class="o">*</span><span class="n">m_context</span><span class="p">);</span> </pre></div></div><p> after handshake we start reading asynchronously: </p> <div class="wiki-code"><div class="code"><pre><span class="n">m_stream</span><span class="o">-&gt;</span><span class="n">async_read_some</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="n">readHandler</span><span class="p">);</span> </pre></div></div><p> After that we just close the connection and remove objects: </p> <div class="wiki-code"><div class="code"><pre><span class="n">m_stream</span><span class="o">-&gt;</span><span class="n">shutdown</span><span class="p">(</span><span class="n">ec</span><span class="p">);</span> <span class="n">m_stream</span><span class="o">-&gt;</span><span class="n">lowest_layer</span><span class="p">().</span><span class="n">close</span><span class="p">(</span><span class="n">ec</span><span class="p">);</span> </pre></div></div><p> Than we destroy the stream and context: </p> <div class="wiki-code"><div class="code"><pre><span class="n">m_stream</span><span class="p">.</span><span class="n">reset</span><span class="p">();</span> <span class="n">m_context</span><span class="p">.</span><span class="n">reset</span><span class="p">();</span> </pre></div></div><p> Close and destroy operation are performed in a context of io_service thread. And after the stream deletion the io_service gets the break assertion: </p> <div class="wiki-code"><div class="code"><pre><span class="o">&gt;</span> <span class="n">msvcp140d</span><span class="p">.</span><span class="n">dll</span><span class="o">!</span><span class="n">std</span><span class="o">::</span><span class="n">_Debug_message</span><span class="p">(</span><span class="k">const</span> <span class="kt">wchar_t</span> <span class="o">*</span> <span class="n">message</span><span class="p">,</span> <span class="k">const</span> <span class="kt">wchar_t</span> <span class="o">*</span> <span class="n">file</span><span class="p">,</span> <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">line</span><span class="p">)</span> <span class="n">Line</span> <span class="mi">17</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_const_iterator</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_val</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Simple_types</span><span class="o">&lt;</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;::</span><span class="k">operator</span><span class="o">*</span><span class="p">()</span> <span class="n">Line</span> <span class="mi">73</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_iterator</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_val</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Simple_types</span><span class="o">&lt;</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;::</span><span class="k">operator</span><span class="o">*</span><span class="p">()</span> <span class="n">Line</span> <span class="mi">332</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">buffer_debug_check</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_iterator</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_val</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Simple_types</span><span class="o">&lt;</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;::</span><span class="k">operator</span><span class="p">()()</span> <span class="n">Line</span> <span class="mi">532</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">std</span><span class="o">::</span><span class="n">_Invoker_functor</span><span class="o">::</span><span class="n">_Call</span><span class="o">&lt;</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">buffer_debug_check</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_iterator</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_val</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Simple_types</span><span class="o">&lt;</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&amp;&gt;</span><span class="p">(</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">buffer_debug_check</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_iterator</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_val</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Simple_types</span><span class="o">&lt;</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&amp;</span> <span class="n">_Obj</span><span class="p">)</span> <span class="n">Line</span> <span class="mi">1377</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">std</span><span class="o">::</span><span class="n">invoke</span><span class="o">&lt;</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">buffer_debug_check</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_iterator</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_val</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Simple_types</span><span class="o">&lt;</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&amp;&gt;</span><span class="p">(</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">buffer_debug_check</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_iterator</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_val</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Simple_types</span><span class="o">&lt;</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&amp;</span> <span class="n">_Obj</span><span class="p">)</span> <span class="n">Line</span> <span class="mi">1443</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">std</span><span class="o">::</span><span class="n">_Invoke_ret</span><span class="o">&lt;</span><span class="kt">void</span><span class="p">,</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">buffer_debug_check</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_iterator</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_val</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Simple_types</span><span class="o">&lt;</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&amp;&gt;</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">_Forced</span><span class="o">&lt;</span><span class="kt">void</span><span class="p">,</span><span class="mi">1</span><span class="o">&gt;</span> <span class="n">__formal</span><span class="p">,</span> <span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">buffer_debug_check</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_iterator</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_val</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Simple_types</span><span class="o">&lt;</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&amp;</span> <span class="o">&lt;</span><span class="n">_Vals_0</span><span class="o">&gt;</span><span class="p">)</span> <span class="n">Line</span> <span class="mi">1461</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">std</span><span class="o">::</span><span class="n">_Func_impl</span><span class="o">&lt;</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">buffer_debug_check</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_iterator</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Vector_val</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">_Simple_types</span><span class="o">&lt;</span><span class="kt">unsigned</span> <span class="kt">char</span><span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span><span class="p">,</span><span class="n">std</span><span class="o">::</span><span class="n">allocator</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">,</span><span class="kt">void</span><span class="o">&gt;::</span><span class="n">_Do_call</span><span class="p">()</span> <span class="n">Line</span> <span class="mi">212</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">std</span><span class="o">::</span><span class="n">_Func_class</span><span class="o">&lt;</span><span class="kt">void</span><span class="o">&gt;::</span><span class="k">operator</span><span class="p">()()</span> <span class="n">Line</span> <span class="mi">279</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">buffer_cast_helper</span><span class="p">(</span><span class="k">const</span> <span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">mutable_buffer</span> <span class="o">&amp;</span> <span class="n">b</span><span class="p">)</span> <span class="n">Line</span> <span class="mi">146</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">buffer_cast</span><span class="o">&lt;</span><span class="kt">void</span> <span class="k">const</span> <span class="o">*&gt;</span><span class="p">(</span><span class="k">const</span> <span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">mutable_buffer</span> <span class="o">&amp;</span> <span class="n">b</span><span class="p">)</span> <span class="n">Line</span> <span class="mi">427</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">buffer_sequence_adapter</span><span class="o">&lt;</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">mutable_buffer</span><span class="p">,</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">mutable_buffers_1</span><span class="o">&gt;::</span><span class="n">validate</span><span class="p">(</span><span class="k">const</span> <span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">mutable_buffers_1</span> <span class="o">&amp;</span> <span class="n">buffer_sequence</span><span class="p">)</span> <span class="n">Line</span> <span class="mi">207</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">win_iocp_socket_recv_op</span><span class="o">&lt;</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">mutable_buffers_1</span><span class="p">,</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">ssl</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">io_op</span><span class="o">&lt;</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">basic_stream_socket</span><span class="o">&lt;</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">ip</span><span class="o">::</span><span class="n">tcp</span><span class="p">,</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">stream_socket_service</span><span class="o">&lt;</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">ip</span><span class="o">::</span><span class="n">tcp</span><span class="o">&gt;</span> <span class="o">&gt;</span><span class="p">,</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">ssl</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">read_op</span><span class="o">&lt;</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">mutable_buffers_1</span><span class="o">&gt;</span><span class="p">,</span><span class="n">std</span><span class="o">::</span><span class="n">function</span><span class="o">&lt;</span><span class="kt">void</span> <span class="kr">__cdecl</span><span class="p">(</span><span class="n">boost</span><span class="o">::</span><span class="n">system</span><span class="o">::</span><span class="n">error_code</span> <span class="k">const</span> <span class="o">&amp;</span><span class="p">,</span><span class="kt">unsigned</span> <span class="kt">int</span><span class="p">)</span><span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;::</span><span class="n">do_complete</span><span class="p">(</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">win_iocp_io_service</span> <span class="o">*</span> <span class="n">owner</span><span class="p">,</span> <span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">win_iocp_operation</span> <span class="o">*</span> <span class="n">base</span><span class="p">,</span> <span class="k">const</span> <span class="n">boost</span><span class="o">::</span><span class="n">system</span><span class="o">::</span><span class="n">error_code</span> <span class="o">&amp;</span> <span class="n">result_ec</span><span class="p">,</span> <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">bytes_transferred</span><span class="p">)</span> <span class="n">Line</span> <span class="mi">72</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">win_iocp_operation</span><span class="o">::</span><span class="n">complete</span><span class="p">(</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">win_iocp_io_service</span> <span class="o">&amp;</span> <span class="n">owner</span><span class="p">,</span> <span class="k">const</span> <span class="n">boost</span><span class="o">::</span><span class="n">system</span><span class="o">::</span><span class="n">error_code</span> <span class="o">&amp;</span> <span class="n">ec</span><span class="p">,</span> <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">bytes_transferred</span><span class="p">)</span> <span class="n">Line</span> <span class="mi">46</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">win_iocp_io_service</span><span class="o">::</span><span class="n">do_one</span><span class="p">(</span><span class="kt">bool</span> <span class="n">block</span><span class="p">,</span> <span class="n">boost</span><span class="o">::</span><span class="n">system</span><span class="o">::</span><span class="n">error_code</span> <span class="o">&amp;</span> <span class="n">ec</span><span class="p">)</span> <span class="n">Line</span> <span class="mi">409</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">win_iocp_io_service</span><span class="o">::</span><span class="n">run</span><span class="p">(</span><span class="n">boost</span><span class="o">::</span><span class="n">system</span><span class="o">::</span><span class="n">error_code</span> <span class="o">&amp;</span> <span class="n">ec</span><span class="p">)</span> <span class="n">Line</span> <span class="mi">164</span> <span class="n">C</span><span class="o">++</span> <span class="n">tls_test</span><span class="p">.</span><span class="n">exe</span><span class="o">!</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">io_service</span><span class="o">::</span><span class="n">run</span><span class="p">(</span><span class="n">boost</span><span class="o">::</span><span class="n">system</span><span class="o">::</span><span class="n">error_code</span> <span class="o">&amp;</span> <span class="n">ec</span><span class="p">)</span> <span class="n">Line</span> <span class="mi">67</span> <span class="n">C</span><span class="o">++</span> </pre></div></div><p> It occurs that completion operation is called after the stream and underlined socket was closed and deleted. The operation is a dynamically object, created by read_some, but it contains link to buffers that application set to it. In case of raw socket the buffers are user application buffers without checkers. In case of ssl the stream uses its internal buffer for TLS operations. And at the moment of the completion operation the stream and its buffers are not exist. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13294 Trac 1.4.3