cpprestsdk icon indicating copy to clipboard operation
cpprestsdk copied to clipboard

Segmentation fault when setting close handler on websocket_callback_client

Open gudbrandtandberg opened this issue 4 years ago • 3 comments

I'm having some trouble setting a close handler when using the websocket_callback_client. The minimal reproducing example below runs fine on windows but segfaults on Linux (CentOS7).

I'm using the following package versions:

  • cpprestsdk-2.10.14
  • boost-1.73.0
  • openssl-1.0.2s

Here is my main file:

#include "cpprest/ws_client.h"

using namespace web;
using namespace web::websockets::client;
using namespace utility;

void
CloseHandler(websocket_close_status close_status, const utility::string_t& reason, const std::error_code& error)
{
  std::cout << "Close handler" << std::endl;
}

void
MessageHandler(websocket_incoming_message msg)
{
  std::cout << "Message handler" << std::endl;
}

int main()
{
    std::cout << "Starting test" << std::endl;

    websocket_callback_client*
      client = new websocket_callback_client();

    client->set_message_handler(MessageHandler);
    client->set_close_handler(CloseHandler);

    auto
      host = uri(conversions::to_string_t("ws://echo.websocket.org"));

    std::cout << "Connecting" << std::endl;
    client->connect(host).wait();

    std::cout << "Closing client" << std::endl;
    client->close().wait();

    std::cout << "Test completed" << std::endl;
    return 0;
}

This is the output:

Starting test
Connecting
Closing client
Segmentation fault

And here is the backtrace:

Thread 4 "test" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff5797700 (LWP 24527)]
0x000000000041aa72 in std::_Function_handler<void (web::websockets::client::websocket_close_status, std::string const&, std::error_code const&), void (*)(web::websockets::client::websocket_close_status, std::string const&, std::error_code const&)>::_M_invoke(std::_Any_data const&, web::websockets::client::websocket_close_status&&, std::string const&, std::error_code const&) ()
Missing separate debuginfos, use: debuginfo-install libgcc-4.8.5-39.el7.x86_64 libstdc++-4.8.5-39.el7.x86_64
(gdb) bt
#0  0x000000000041aa72 in std::_Function_handler<void (web::websockets::client::websocket_close_status, std::string const&, std::error_code const&), void (*)(web::websockets::client::websocket_close_status, std::string const&, std::error_code const&)>::_M_invoke(std::_Any_data const&, web::websockets::client::websocket_close_status&&, std::string const&, std::error_code const&) ()
#1  0x0000000000469cc9 in std::_Function_handler<void (), void web::websockets::client::details::wspp_callback_client::shutdown_wspp_impl<websocketpp::config::asio_client>(std::weak_ptr<void> const&, bool)::{lambda()#1}>::_M_invoke(std::_Any_data const&) ()
#2  0x00000000004171c2 in std::function<void ()>::operator()() const ()
#3  0x000000000043cadc in std::_Function_handler<unsigned char (), pplx::details::_MakeVoidToUnitFunc(std::function<void ()> const&)::{lambda()#1}>::_M_invoke(std::_Any_data const&) ()
#4  0x00000000004cf584 in pplx::task<unsigned char>::_InitialTaskHandle<void, void web::websockets::client::details::wspp_callback_client::shutdown_wspp_impl<websocketpp::config::asio_client>(std::weak_ptr<void> const&, bool)::{lambda()#1}, pplx::details::_TypeSelectorNoAsync>::_Init(pplx::details) const ()
#5  0x00000000004cf780 in pplx::details::_PPLTaskHandle<unsigned char, pplx::task<unsigned char>::_InitialTaskHandle<void, void web::websockets::client::details::wspp_callback_client::shutdown_wspp_impl<websocketpp::config::asio_client>(std::weak_ptr<void> const&, bool)::{lambda()#1}, pplx::details::_TypeSelectorNoAsync>, pplx::details::_TaskProcHandle>::invoke() const ()
#6  0x0000000000412709 in pplx::details::_TaskProcHandle::_RunChoreBridge(void*) ()
---Type <return> to continue, or q <return> to quit---
#7  0x00000000004dc191 in boost::asio::detail::completion_handler<boost::_bi::bind_t<void, void (*)(void*), boost::_bi::list1<boost::_bi::value<void*> > > >::do_complete(void*, boost::asio::detail::scheduler_operation*, boost::system::error_code const&, unsigned long) ()
#8  0x000000000043f3f2 in boost::asio::detail::scheduler::run(boost::system::error_code&) ()
#9  0x00000000004dc9dd in boost::asio::detail::posix_thread::func<(anonymous namespace)::threadpool_impl::add_thread()::{lambda()#1}>::run() ()
#10 0x000000000042d83f in boost_asio_detail_posix_thread_function ()
#11 0x00007ffff7bc6ea5 in start_thread () from /lib64/libpthread.so.0
#12 0x00007ffff6ecc8dd in clone () from /lib64/libc.so.6

gudbrandtandberg avatar Jan 11 '21 10:01 gudbrandtandberg

Hi. I'm evaluating websocket_callback_client so I got curious when I saw this ticket. Have you had any more insights about the issue? You could also try to run the existing functional cpprestsdk unit tests, they have something for the callback client too.

The code works for me under current macOS, but against my local testing endpoint.

sebkraemer avatar Sep 16 '21 11:09 sebkraemer

Hey, after a lot of debugging I did not manage to get any more insight.. The failure only happened for me on linux (centos7), the same code runs fine on windows. Gudbrand

On Thu, Sep 16, 2021 at 1:01 PM Sebastian Krämer @.***> wrote:

Hi. I'm evaluating websocket_callback_client so I got curious when I saw this ticket. Have you had any more insights about the issue? You could also try to run the existing functional cpprestsdk unit tests, they have something for the callback client too.

The code works for me under current macOS, but against my local testing endpoint.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/microsoft/cpprestsdk/issues/1564#issuecomment-920799902, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABSDGR54GJ6JQPEK3QRW3UTUCHE7XANCNFSM4V5KN5IQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

gudbrandtandberg avatar Sep 16 '21 11:09 gudbrandtandberg

OK. Thank you for the quick response.

sebkraemer avatar Sep 16 '21 11:09 sebkraemer