Skip to content

Commit 4e1d5b8

Browse files
JoelKatzvinniefalco
authored andcommitted
Fix the two bugs that are causing us the most pain. If we encounter an error while processing or handling a connection, don't destroy the endpoint.
1 parent 6aa11a7 commit 4e1d5b8

File tree

1 file changed

+45
-30
lines changed

1 file changed

+45
-30
lines changed

src/roles/server.hpp

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,14 @@ void server<endpoint>::start_listen(const boost::asio::ip::tcp::endpoint& e,size
301301

302302
if(num_threads == 0)
303303
{
304-
m_endpoint.run_internal();
304+
for (;;) {
305+
try {
306+
m_endpoint.run_internal();
307+
break;
308+
} catch (const std::exception & e) { // Don't let an exception trash the endpoint DJS
309+
m_endpoint.elog().at(log::elevel::RERROR) << "run_internal exception: " << e.what() << log::endl;
310+
}
311+
}
305312
m_state = IDLE;
306313
}
307314
}
@@ -382,35 +389,43 @@ void server<endpoint>::handle_accept(connection_ptr con,
382389
const boost::system::error_code& error)
383390
{
384391
boost::lock_guard<boost::recursive_mutex> lock(m_endpoint.m_lock);
385-
386-
if (error) {
387-
con->m_fail_code = fail::status::SYSTEM;
388-
con->m_fail_system = error;
389-
390-
if (error == boost::system::errc::too_many_files_open) {
391-
con->m_fail_reason = "too many files open";
392-
393-
// TODO: make this configurable
394-
//m_timer.expires_from_now(boost::posix_time::milliseconds(1000));
395-
//m_timer.async_wait(boost::bind(&type::start_accept,this));
396-
} else if (error == boost::asio::error::operation_aborted) {
397-
con->m_fail_reason = "io_service operation canceled";
398-
399-
// the operation was canceled. This was probably due to the
400-
// io_service being stopped.
401-
} else {
402-
con->m_fail_reason = "unknown";
403-
}
404-
405-
m_endpoint.m_elog->at(log::elevel::RERROR)
406-
<< "async_accept returned error: " << error
407-
<< " (" << con->m_fail_reason << ")" << log::endl;
408-
409-
con->terminate(false);
410-
} else {
411-
con->start();
412-
}
413-
392+
393+
try
394+
{
395+
if (error) {
396+
con->m_fail_code = fail::status::SYSTEM;
397+
con->m_fail_system = error;
398+
399+
if (error == boost::system::errc::too_many_files_open) {
400+
con->m_fail_reason = "too many files open";
401+
402+
// TODO: make this configurable
403+
//m_timer.expires_from_now(boost::posix_time::milliseconds(1000));
404+
//m_timer.async_wait(boost::bind(&type::start_accept,this));
405+
} else if (error == boost::asio::error::operation_aborted) {
406+
con->m_fail_reason = "io_service operation canceled";
407+
408+
// the operation was canceled. This was probably due to the
409+
// io_service being stopped.
410+
} else {
411+
con->m_fail_reason = "unknown";
412+
}
413+
414+
m_endpoint.m_elog->at(log::elevel::RERROR)
415+
<< "async_accept returned error: " << error
416+
<< " (" << con->m_fail_reason << ")" << log::endl;
417+
418+
con->terminate(false);
419+
} else {
420+
con->start();
421+
}
422+
}
423+
catch (const std::exception & e)
424+
{ // We must call start_accept, even if we throw DJS
425+
m_endpoint.m_elog->at(log::elevel::RERROR)
426+
<< "handle_accept caught exception: " << e.what() << log::endl;
427+
}
428+
414429
this->start_accept();
415430
}
416431

0 commit comments

Comments
 (0)