fix closing streams if an error happens

This commit is contained in:
bernatx 2023-06-09 15:57:03 +02:00 committed by bernat
parent 2b78e09db2
commit d5fb48eb5f
4 changed files with 29 additions and 22 deletions

View File

@ -81,7 +81,7 @@ namespace multigpu {
if (!self) return; if (!self) return;
if (ec) { if (ec) {
log_error("session ", self->_session_id, ": error sending data: ", ec.message()); log_error("session ", self->_session_id, ": error sending data: ", ec.message());
self->CloseNow(); self->CloseNow(ec);
} else { } else {
// DEBUG_ASSERT_EQ(bytes, sizeof(message_size_type) + message->size()); // DEBUG_ASSERT_EQ(bytes, sizeof(message_size_type) + message->size());
} }
@ -200,15 +200,19 @@ namespace multigpu {
} }
} }
void Primary::CloseNow() { void Primary::CloseNow(boost::system::error_code ec) {
_deadline.cancel(); _deadline.cancel();
if (!ec)
{
if (_socket.is_open()) { if (_socket.is_open()) {
boost::system::error_code ec; boost::system::error_code ec2;
_socket.shutdown(boost::asio::socket_base::shutdown_both, ec); _socket.shutdown(boost::asio::socket_base::shutdown_both, ec2);
_socket.close(); _socket.close();
_on_closed(shared_from_this());
} }
} }
_on_closed(shared_from_this());
log_debug("session", _session_id, "closed");
}
} // namespace multigpu } // namespace multigpu
} // namespace carla } // namespace carla

View File

@ -80,7 +80,7 @@ namespace multigpu {
void StartTimer(); void StartTimer();
void CloseNow(); void CloseNow(boost::system::error_code ec = boost::system::error_code());
friend class Listener; friend class Listener;

View File

@ -62,7 +62,7 @@ namespace tcp {
boost::asio::post(_strand.context(), [=]() { callback(self); }); boost::asio::post(_strand.context(), [=]() { callback(self); });
} else { } else {
log_error("session", _session_id, ": error retrieving stream id :", ec.message()); log_error("session", _session_id, ": error retrieving stream id :", ec.message());
CloseNow(); CloseNow(ec);
} }
}; };
@ -101,7 +101,7 @@ namespace tcp {
_is_writing = false; _is_writing = false;
if (ec) { if (ec) {
log_info("session", _session_id, ": error sending data :", ec.message()); log_info("session", _session_id, ": error sending data :", ec.message());
CloseNow(); CloseNow(ec);
} else { } else {
DEBUG_ONLY(log_debug("session", _session_id, ": successfully sent", bytes, "bytes")); DEBUG_ONLY(log_debug("session", _session_id, ": successfully sent", bytes, "bytes"));
DEBUG_ASSERT_EQ(bytes, sizeof(message_size_type) + message->size()); DEBUG_ASSERT_EQ(bytes, sizeof(message_size_type) + message->size());
@ -137,13 +137,16 @@ namespace tcp {
} }
} }
void ServerSession::CloseNow() { void ServerSession::CloseNow(boost::system::error_code ec) {
_deadline.cancel(); _deadline.cancel();
if (!ec)
{
if (_socket.is_open()) { if (_socket.is_open()) {
boost::system::error_code ec; boost::system::error_code ec2;
_socket.shutdown(boost::asio::socket_base::shutdown_both, ec); _socket.shutdown(boost::asio::socket_base::shutdown_both, ec2);
_socket.close(); _socket.close();
} }
}
_on_closed(shared_from_this()); _on_closed(shared_from_this());
log_debug("session", _session_id, "closed"); log_debug("session", _session_id, "closed");
} }

View File

@ -88,7 +88,7 @@ namespace tcp {
void StartTimer(); void StartTimer();
void CloseNow(); void CloseNow(boost::system::error_code ec = boost::system::error_code());
friend class Server; friend class Server;