@@ -35,7 +35,7 @@ namespace signalr
3535 connection_impl::connection_impl (const std::string& url, trace_level trace_level, const std::shared_ptr<log_writer>& log_writer,
3636 std::function<std::shared_ptr<http_client>(const signalr_client_config&)> http_client_factory, std::function<std::shared_ptr<websocket_client>(const signalr_client_config&)> websocket_factory, const bool skip_negotiation)
3737 : m_base_url(url), m_connection_state(connection_state::disconnected), m_logger(log_writer, trace_level), m_transport(nullptr ), m_skip_negotiation(skip_negotiation),
38- m_message_received ([](const std::string&) noexcept {}), m_disconnected([](std::exception_ptr) noexcept {}), m_disconnect_cts(std::make_shared<cancellation_token >())
38+ m_message_received ([](const std::string&) noexcept {}), m_disconnected([](std::exception_ptr) noexcept {}), m_disconnect_cts(std::make_shared<cancellation_token_source >())
3939 {
4040 if (http_client_factory != nullptr )
4141 {
@@ -65,7 +65,18 @@ namespace signalr
6565 // Signaling the event is safe here. We are in the dtor so noone is using this instance. There might be some
6666 // outstanding threads that hold on to the connection via a weak pointer but they won't be able to acquire
6767 // the instance since it is being destroyed. Note that the event may actually be in non-signaled state here.
68- m_start_completed_event.cancel ();
68+ try
69+ {
70+ m_start_completed_event.cancel ();
71+ }
72+ catch (const std::exception& ex)
73+ {
74+ if (m_logger.is_enabled (trace_level::warning))
75+ {
76+ m_logger.log (trace_level::warning, std::string (" start completed event threw an exception in the destructor: " )
77+ .append (ex.what ()));
78+ }
79+ }
6980 completion_event completion;
7081 auto logger = m_logger;
7182 shutdown ([completion, logger](std::exception_ptr exception) mutable
@@ -194,7 +205,18 @@ namespace signalr
194205
195206 connection->m_transport = nullptr ;
196207 connection->change_state (connection_state::disconnected);
197- connection->m_start_completed_event .cancel ();
208+ try
209+ {
210+ connection->m_start_completed_event .cancel ();
211+ }
212+ catch (const std::exception& ex)
213+ {
214+ if (connection->m_logger .is_enabled (trace_level::warning))
215+ {
216+ connection->m_logger .log (trace_level::warning, std::string (" start completed event threw an exception in start negotiate: " )
217+ .append (ex.what ()));
218+ }
219+ }
198220 callback (std::current_exception ());
199221 return ;
200222 }
@@ -213,7 +235,18 @@ namespace signalr
213235 assert (false );
214236 }
215237
216- connection->m_start_completed_event .cancel ();
238+ try
239+ {
240+ connection->m_start_completed_event .cancel ();
241+ }
242+ catch (const std::exception& ex)
243+ {
244+ if (connection->m_logger .is_enabled (trace_level::warning))
245+ {
246+ connection->m_logger .log (trace_level::warning, std::string (" start completed event threw an exception in start negotiate: " )
247+ .append (ex.what ()));
248+ }
249+ }
217250 callback (nullptr );
218251 };
219252
@@ -328,7 +361,7 @@ namespace signalr
328361 }
329362
330363 connection->start_transport (url, transport_started);
331- });
364+ }, get_cancellation_token (m_disconnect_cts) );
332365 }
333366
334367 void connection_impl::start_transport (const std::string& url, std::function<void (std::shared_ptr<transport>, std::exception_ptr)> transport_started)
@@ -552,7 +585,18 @@ namespace signalr
552585 const auto current_state = get_connection_state ();
553586 if (current_state == connection_state::disconnected)
554587 {
555- m_disconnect_cts->cancel ();
588+ try
589+ {
590+ m_disconnect_cts->cancel ();
591+ }
592+ catch (const std::exception& ex)
593+ {
594+ if (m_logger.is_enabled (trace_level::warning))
595+ {
596+ m_logger.log (trace_level::warning, std::string (" disconnect event threw an exception in shutdown: " )
597+ .append (ex.what ()));
598+ }
599+ }
556600 callback (m_stop_error);
557601 return ;
558602 }
@@ -567,7 +611,18 @@ namespace signalr
567611 }
568612
569613 // we request a cancellation of the ongoing start (if any) and wait until it is canceled
570- m_disconnect_cts->cancel ();
614+ try
615+ {
616+ m_disconnect_cts->cancel ();
617+ }
618+ catch (const std::exception& ex)
619+ {
620+ if (m_logger.is_enabled (trace_level::warning))
621+ {
622+ m_logger.log (trace_level::warning, std::string (" disconnect event threw an exception in shutdown: " )
623+ .append (ex.what ()));
624+ }
625+ }
571626
572627 while (m_start_completed_event.wait (60000 ) != 0 )
573628 {
0 commit comments