From 616b6c730ad8f04b9a458213a57b1c163d4436d6 Mon Sep 17 00:00:00 2001 From: Bryan Biedenkapp Date: Mon, 25 May 2026 14:01:09 -0400 Subject: [PATCH] fix the shared_from_this lifetime in for ServerConnection and SecureServerConnection; --- .../restapi/http/SecureServerConnection.h | 25 ++++++++----------- src/common/restapi/http/ServerConnection.h | 19 ++++++-------- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/common/restapi/http/SecureServerConnection.h b/src/common/restapi/http/SecureServerConnection.h index 32dcd4a6..801fdc0e 100644 --- a/src/common/restapi/http/SecureServerConnection.h +++ b/src/common/restapi/http/SecureServerConnection.h @@ -106,11 +106,9 @@ namespace restapi */ void handshake() { - if (!m_persistent) { - auto self(this->shared_from_this()); - } + auto self = this->shared_from_this(); - m_socket.async_handshake(asio::ssl::stream_base::server, [this](asio::error_code ec) { + m_socket.async_handshake(asio::ssl::stream_base::server, [this, self](asio::error_code ec) { if (!ec) { read(); } @@ -122,11 +120,8 @@ namespace restapi */ void read() { - if (!m_persistent) { - auto self(this->shared_from_this()); - } - - m_socket.async_read_some(asio::buffer(m_buffer), [=](asio::error_code ec, std::size_t recvLength) { + auto self = this->shared_from_this(); + m_socket.async_read_some(asio::buffer(m_buffer), [this, self](asio::error_code ec, std::size_t recvLength) { if (!ec) { HTTPLexer::ResultType result = HTTPLexer::GOOD; char* content; @@ -211,7 +206,7 @@ namespace restapi if (ec) { ::LogError(LOG_REST, "SecureServerConnection::read(), %s, code = %u", ec.message().c_str(), ec.value()); } - m_connectionManager.stop(this->shared_from_this()); + m_connectionManager.stop(self); m_continue = false; } }); @@ -222,14 +217,14 @@ namespace restapi */ void write() { - if (!m_persistent) { - auto self(this->shared_from_this()); - } else { + auto self = this->shared_from_this(); + + if (m_persistent) { m_reply.headers.add("Connection", "keep-alive"); } auto buffers = m_reply.toBuffers(); - asio::async_write(m_socket, buffers, [=](asio::error_code ec, std::size_t) { + asio::async_write(m_socket, buffers, [this, self](asio::error_code ec, std::size_t) { if (m_persistent) { m_lexer.reset(); m_reply.headers = HTTPHeaders(); @@ -253,7 +248,7 @@ namespace restapi if (ec) { ::LogError(LOG_REST, "SecureServerConnection::write(), %s, code = %u", ec.message().c_str(), ec.value()); } - m_connectionManager.stop(this->shared_from_this()); + m_connectionManager.stop(self); } } }); diff --git a/src/common/restapi/http/ServerConnection.h b/src/common/restapi/http/ServerConnection.h index d5195341..98bbf2bc 100644 --- a/src/common/restapi/http/ServerConnection.h +++ b/src/common/restapi/http/ServerConnection.h @@ -103,11 +103,8 @@ namespace restapi */ void read() { - if (!m_persistent) { - auto self(this->shared_from_this()); - } - - m_socket.async_read_some(asio::buffer(m_buffer), [=](asio::error_code ec, std::size_t recvLength) { + auto self = this->shared_from_this(); + m_socket.async_read_some(asio::buffer(m_buffer), [this, self](asio::error_code ec, std::size_t recvLength) { if (!ec) { HTTPLexer::ResultType result = HTTPLexer::GOOD; char* content; @@ -192,7 +189,7 @@ namespace restapi if (ec) { ::LogError(LOG_REST, "ServerConnection::read(), %s, code = %u", ec.message().c_str(), ec.value()); } - m_connectionManager.stop(this->shared_from_this()); + m_connectionManager.stop(self); m_continue = false; m_contResult = HTTPLexer::INDETERMINATE; } @@ -204,14 +201,14 @@ namespace restapi */ void write() { - if (!m_persistent) { - auto self(this->shared_from_this()); - } else { + auto self = this->shared_from_this(); + + if (m_persistent) { m_reply.headers.add("Connection", "keep-alive"); } auto buffers = m_reply.toBuffers(); - asio::async_write(m_socket, buffers, [=](asio::error_code ec, std::size_t) { + asio::async_write(m_socket, buffers, [this, self](asio::error_code ec, std::size_t) { if (m_persistent) { m_lexer.reset(); m_reply.headers = HTTPHeaders(); @@ -235,7 +232,7 @@ namespace restapi if (ec) { ::LogError(LOG_REST, "ServerConnection::write(), %s, code = %u", ec.message().c_str(), ec.value()); } - m_connectionManager.stop(this->shared_from_this()); + m_connectionManager.stop(self); } } });