cpprestsdk
cpprestsdk copied to clipboard
Thread Safety
Is it safe to create a websocket_callback_client
and setup the callback function in the main thread, then call websocket_callback_client::send()
from a separate thread?
I'm getting an unhandled exception _REPORT_PPLTASK_UNOBSERVED_EXCEPTION()
and I believe this is why.
I have a timer class that calls a function at a set interval, the calling of this function is handled by a worker std::thread
. The function contains a call to websocket_callback_client::send()
and the unhandled exception is pointing to this call. Sometimes the function crashes the application on the first call, sometimes on the second call. This leads me to believe it is a race condition.
The worker threads calling function is simply:
void OnTick()
{
printf("Heartbeat ");
rapidjson::StringBuffer OutJSON;
rapidjson::Writer<rapidjson::StringBuffer> writer(OutJSON);
writer.StartObject();
writer.Key("op");
writer.Int(1);
writer.Key("d");
writer.Null();
writer.EndObject();
websocket_outgoing_message msg;
msg.set_utf8_message(OutJSON.GetString());
wsClient.send(msg).then([=]() { printf("Sent\n"); });
}
_M_stackTrace:
{[capacity]=10 [allocator]=allocator [0]=0x00007ff82ebb89d3 {cpprest_2_10.dll!web::websockets::client::websocket_outgoing_message::signal_body_sent(const std::exception_ptr & e), Line 150} ...} Concurrency::details::_TaskCreationCallstack
[0] 0x00007ff82ebb89d3 {cpprest_2_10.dll!web::websockets::client::websocket_outgoing_message::signal_body_sent(const std::exception_ptr & e), Line 150} void *
[1] 0x00007ff82eaf869a {cpprest_2_10.dll!web::websockets::client::details::wspp_callback_client::send_msg::__l2::<lambda>(Concurrency::task<std::error_code> previousTask), Line 561} void *
[2] 0x00007ff82ea1ac40 {cpprest_2_10.dll!std::_Invoker_functor::_Call<void <lambda>(Concurrency::task<std::error_code>) & __ptr64,Concurrency::task<std::error_code> >(web::websockets::client::details::wspp_callback_client::send_msg::__l2::void <lambda>(Concurrency::task<std::error_code>) & _Obj, Concurrency::task<std::error_code> && <_Args_0>), Line 1790} void *
[3] 0x00007ff82ea865ac {cpprest_2_10.dll!std::invoke<void <lambda>(Concurrency::task<std::error_code>) & __ptr64,Concurrency::task<std::error_code> >(web::websockets::client::details::wspp_callback_client::send_msg::__l2::void <lambda>(Concurrency::task<std::error_code>) & _Obj, Concurrency::task<std::error_code> && <_Args_0>), Line 1790} void *
[4] 0x00007ff82ea1abbc {cpprest_2_10.dll!std::_Invoker_ret<void,1>::_Call<void <lambda>(Concurrency::task<std::error_code>) & __ptr64,Concurrency::task<std::error_code> >(web::websockets::client::details::wspp_callback_client::send_msg::__l2::void <lambda>(Concurrency::task<std::error_code>) & <_Vals_0>, Concurrency::task<std::error_code> && <_Vals_1>), Line 1816} void *
[5] 0x00007ff82eb2542e {cpprest_2_10.dll!std::_Func_impl_no_alloc<void <lambda>(Concurrency::task<std::error_code>),void,Concurrency::task<std::error_code> >::_Do_call(Concurrency::task<std::error_code> && <_Args_0>), Line 300} void *
[6] 0x00007ff82eafd1f8 {cpprest_2_10.dll!std::_Func_class<void,Concurrency::task<std::error_code> >::operator()(Concurrency::task<std::error_code> <_Args_0>), Line 350} void *
[7] 0x00007ff82eafc12c {cpprest_2_10.dll!Concurrency::details::_MakeTToUnitFunc::__l2::<lambda>(Concurrency::task<std::error_code> _Obj), Line 2313} void *
[8] 0x00007ff82ea1db70 {cpprest_2_10.dll!std::_Invoker_functor::_Call<unsigned char <lambda>(Concurrency::task<std::error_code>) & __ptr64,Concurrency::task<std::error_code> >(Concurrency::details::_MakeTToUnitFunc::__l2::unsigned char <lambda>(Concurrency::task<std::error_code>) & _Obj, Concurrency::task<std::error_code> && <_Args_0>), Line 1790} void *
[9] 0x00007ff82ea87cac {cpprest_2_10.dll!std::invoke<unsigned char <lambda>(Concurrency::task<std::error_code>) & __ptr64,Concurrency::task<std::error_code> >(Concurrency::details::_MakeTToUnitFunc::__l2::unsigned char <lambda>(Concurrency::task<std::error_code>) & _Obj, Concurrency::task<std::error_code> && <_Args_0>), Line 1790} void *
the same problem with me, solved yet? any clue?
No, this left me at an impasse and I was forced to abandon the library.