Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling in on_accept #750

Merged
merged 4 commits into from
Aug 3, 2018
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions Release/src/http/listener/http_server_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,28 +540,47 @@ will_deref_and_erase_t asio_server_connection::start_request_response()

void hostport_listener::on_accept(ip::tcp::socket* socket, const boost::system::error_code& ec)
{
std::unique_ptr<ip::tcp::socket> usocket(std::move(socket));
// Listener closed
if (ec == boost::asio::error::operation_aborted)
{
return;
}

// Handle successful accept
if (!ec)
{
std::unique_ptr<ip::tcp::socket> usocket(std::move(socket));
auto conn = new asio_server_connection(std::move(usocket), m_p_server, this);

std::lock_guard<std::mutex> lock(m_connections_lock);
m_connections.insert(conn);
conn->start(m_is_https, m_ssl_context_callback);
if (m_connections.size() == 1)
m_all_connections_complete.reset();
try
{
// Take a lock here so it released before call to internal_erase_connection
std::lock_guard<std::mutex> lock(m_connections_lock);

if (m_acceptor)
m_connections.insert(conn);
conn->start(m_is_https, m_ssl_context_callback);
if (m_connections.size() == 1)
m_all_connections_complete.reset();
}
catch (boost::system::system_error&)
{
// spin off another async accept
auto newSocket = new ip::tcp::socket(crossplat::threadpool::shared_instance().service());
m_acceptor->async_accept(*newSocket, [this, newSocket](const boost::system::error_code& ec)
{
this->on_accept(newSocket, ec);
});
// boost ssl apis throw boost::system::system_error.
// Exception indicates something went wrong setting ssl context.
// Drop connection and continue handling other connections.
internal_erase_connection(conn);
}
}

std::lock_guard<std::mutex> lock(m_connections_lock);
if (m_acceptor)
{
// spin off another async accept
auto newSocket = new ip::tcp::socket(crossplat::threadpool::shared_instance().service());
m_acceptor->async_accept(*newSocket, [this, newSocket](const boost::system::error_code& ec)
{
this->on_accept(newSocket, ec);
});
}
}

will_deref_and_erase_t asio_server_connection::handle_http_line(const boost::system::error_code& ec)
Expand Down