title: C++ Rest SDK Casablanca Sigtrap
body: <p>I am using the <a href="https://github.com/Microsoft/cpprestsdk" rel="nofollow">C++ Rest SDK ("Casablanca")</a> to receive feed from Websocket-Servers. Currently, I have three different connections to three different servers running at the same time using the <a href="http://microsoft.github.io/cpprestsdk/classweb_1_1websockets_1_1client_1_1websocket__callback__client.html" rel="nofollow">websocket_callback_client class</a>. </p>
  
  <p>The program runs for an undefined time and then suddenly receives <code>SIGTRAP, Trace/ Breakpoint trap</code>. This is the output of <code>GDB</code>:</p>
  
  <pre><code>#0  0x00007ffff5abec37 in __GI_raise (sig=5) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
  #1  0x000000000047bb8e in pplx::details::_ExceptionHolder::~_ExceptionHolder() ()
  #2  0x000000000044be29 in std::_Sp_counted_base&lt;(__gnu_cxx::_Lock_policy)2&gt;::_M_release() ()
  #3  0x000000000047fa39 in pplx::details::_Task_impl&lt;unsigned char&gt;::~_Task_impl() ()
  #4  0x000000000044be29 in std::_Sp_counted_base&lt;(__gnu_cxx::_Lock_policy)2&gt;::_M_release() ()
  #5  0x00007ffff6feb09f in std::__shared_count&lt;(__gnu_cxx::_Lock_policy)2&gt;::~__shared_count (this=0x7fffc8021420, __in_chrg=&lt;optimized out&gt;) at /usr/include/c++/4.8/bits/shared_ptr_base.h:546
  #6  0x00007ffff6fffa38 in std::__shared_ptr&lt;pplx::details::_Task_impl&lt;unsigned char&gt;, (__gnu_cxx::_Lock_policy)2&gt;::~__shared_ptr (this=0x7fffc8021418, __in_chrg=&lt;optimized out&gt;) at /usr/include/c++/4.8/bits/shared_ptr_base.h:781
  #7  0x00007ffff6fffa52 in std::shared_ptr&lt;pplx::details::_Task_impl&lt;unsigned char&gt; &gt;::~shared_ptr (this=0x7fffc8021418, __in_chrg=&lt;optimized out&gt;) at /usr/include/c++/4.8/bits/shared_ptr.h:93
  #8  0x00007ffff710f766 in pplx::details::_PPLTaskHandle&lt;unsigned char, pplx::task&lt;unsigned char&gt;::_InitialTaskHandle&lt;void, void web::websockets::client::details::wspp_callback_client::shutdown_wspp_impl&lt;websocketpp::config::asio_tls_client&gt;(std::weak_ptr&lt;void&gt; const&amp;, bool)::{lambda()#1}, pplx::details::_TypeSelectorNoAsync&gt;, pplx::details::_TaskProcHandle&gt;::~_PPLTaskHandle() (this=0x7fffc8021410, __in_chrg=&lt;optimized out&gt;)
      at /home/cpprestsdk/Release/include/pplx/pplxtasks.h:1631
  #9  0x00007ffff716e6f2 in pplx::task&lt;unsigned char&gt;::_InitialTaskHandle&lt;void, void web::websockets::client::details::wspp_callback_client::shutdown_wspp_impl&lt;websocketpp::config::asio_tls_client&gt;(std::weak_ptr&lt;void&gt; const&amp;, bool)::{lambda()#1}, pplx::details::_TypeSelectorNoAsync&gt;::~_InitialTaskHandle() (this=0x7fffc8021410, __in_chrg=&lt;optimized out&gt;) at /home/cpprestsdk/Release/include/pplx/pplxtasks.h:3710
  #10 0x00007ffff716e722 in pplx::task&lt;unsigned char&gt;::_InitialTaskHandle&lt;void, void web::websockets::client::details::wspp_callback_client::shutdown_wspp_impl&lt;websocketpp::config::asio_tls_client&gt;(std::weak_ptr&lt;void&gt; const&amp;, bool)::{lambda()#1}, pplx::details::_TypeSelectorNoAsync&gt;::~_InitialTaskHandle() (this=0x7fffc8021410, __in_chrg=&lt;optimized out&gt;) at /home/cpprestsdk/Release/include/pplx/pplxtasks.h:3710
  #11 0x00007ffff71f9cdd in boost::_bi::list1&lt;boost::_bi::value&lt;void*&gt; &gt;::operator()&lt;void (*)(void*), boost::_bi::list0&gt; (this=0x7fffdc7d7d28, f=@0x7fffdc7d7d20: 0x479180 &lt;pplx::details::_TaskProcHandle::_RunChoreBridge(void*)&gt;, a=...)
      at /usr/local/include/boost/bind/bind.hpp:259
  #12 0x00007ffff71f9c8f in boost::_bi::bind_t&lt;void, void (*)(void*), boost::_bi::list1&lt;boost::_bi::value&lt;void*&gt; &gt; &gt;::operator() (this=0x7fffdc7d7d20) at /usr/local/include/boost/bind/bind.hpp:1222
  #13 0x00007ffff71f9c54 in boost::asio::asio_handler_invoke&lt;boost::_bi::bind_t&lt;void, void (*)(void*), boost::_bi::list1&lt;boost::_bi::value&lt;void*&gt; &gt; &gt; &gt; (function=...) at /usr/local/include/boost/asio/handler_invoke_hook.hpp:69
  #14 0x00007ffff71f9bea in boost_asio_handler_invoke_helpers::invoke&lt;boost::_bi::bind_t&lt;void, void (*)(void*), boost::_bi::list1&lt;boost::_bi::value&lt;void*&gt; &gt; &gt;, boost::_bi::bind_t&lt;void, void (*)(void*), boost::_bi::list1&lt;boost::_bi::value&lt;void*&gt; &gt; &gt; &gt; (function=..., context=...) at /usr/local/include/boost/asio/detail/handler_invoke_helpers.hpp:37
  #15 0x00007ffff71f9b2e in boost::asio::detail::completion_handler&lt;boost::_bi::bind_t&lt;void, void (*)(void*), boost::_bi::list1&lt;boost::_bi::value&lt;void*&gt; &gt; &gt; &gt;::do_complete (owner=0x7488d0, base=0x7fffc801ecd0)
      at /usr/local/include/boost/asio/detail/completion_handler.hpp:68
  #16 0x00000000004c34c1 in boost::asio::detail::task_io_service::run(boost::system::error_code&amp;) ()
  #17 0x00007ffff709fb27 in boost::asio::io_service::run (this=0x7ffff759ab78 &lt;crossplat::threadpool::shared_instance()::s_shared+24&gt;) at /usr/local/include/boost/asio/impl/io_service.ipp:59
  #18 0x00007ffff7185a81 in crossplat::threadpool::thread_start (arg=0x7ffff759ab60 &lt;crossplat::threadpool::shared_instance()::s_shared&gt;) at /home/cpprestsdk/Release/include/pplx/threadpool.h:133
  #19 0x00007ffff566e184 in start_thread (arg=0x7fffdc7d8700) at pthread_create.c:312
  #20 0x00007ffff5b8237d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
  </code></pre>
  
  <p>At line #18 the soruce <a href="https://microsoft.github.io/cpprestsdk/threadpool_8h_source.html" rel="nofollow">/pplx/threadpool.h:133</a> is given.
  This is the source-code around these lines:</p>
  
  <pre><code>  123     static void* thread_start(void *arg)
    124     {
    125 #if (defined(ANDROID) || defined(__ANDROID__))
    126         // Calling get_jvm_env() here forces the thread to be attached.
    127         get_jvm_env();
    128         pthread_cleanup_push(detach_from_java, nullptr);
    129 #endif
    130         threadpool* _this = reinterpret_cast&lt;threadpool*&gt;(arg);
    131         try
    132         {
    133             _this-&gt;m_service.run();
    134         }
    135         catch (const _cancel_thread&amp;)
    136         {
    137             // thread was cancelled
    138         }
    139         catch (...)
    140         {
    141             // Something bad happened
    142 #if (defined(ANDROID) || defined(__ANDROID__))
    143             // Reach into the depths of the 'droid!
    144             // NOTE: Uses internals of the bionic library
    145             // Written against android ndk r9d, 7/26/2014
    146             __pthread_cleanup_pop(&amp;__cleanup, true);
    147             throw;
    148 #endif
    149         }
    150 #if (defined(ANDROID) || defined(__ANDROID__))
    151         pthread_cleanup_pop(true);
    152 #endif
    153         return arg;
    154     }
  </code></pre>
  
  <p>For clarification, <code>m_service</code> is a <code>boost::asio::io_service</code>.
  To me it looks like line #133 throws an exception, it gets caught at line #139 and then rethrown. At this point, I have to catch it personally, because if I don't and the <code>pplx</code>-object gets destroyed with an uncaught exception, it will raise <code>SIGTRAP</code>. </p>
  
  <p>This is how far I got with my research. The problem is I do not have a clue where this is happening. I have surrounded every position where data is sent through or received from <code>websocket_callback_client</code> with <code>try {} catch(...){}</code> and it is still happening.</p>
  
  <p>Maybe someone who has used this library before can help me out.</p>