chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.96k stars 1.22k forks source link

Crashes for an unhandled exception from socket.remote_endpoint(). #1012

Open SiyiJia opened 2 years ago

SiyiJia commented 2 years ago

My program crashes in the following call stack.

[ 00 ] __GI_raise
[ 01 ] __GI_abort
[ 02 ] __gnu_cxx::__verbose_terminate_handler()
[ 03 ] __cxxabiv1::__terminate(void (*)())
[ 04 ] std::terminate()
[ 05 ] __cxxabiv1::__cxa_throw
[ 06 ] void boost::throw_exception<boost::system::system_error>(boost::system::system_error const&)
[ 07 ] boost::asio::detail::do_throw_error(boost::system::error_code const&, char const*)
[ 08 ] (anonymous namespace)::asio_server_connection::handle_http_line(boost::system::error_code const&)
[ 09 ] boost::asio::detail::read_until_match_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp>, boost::asio::basic_streambuf_ref<std::allocator<char> >, (anonymous namespace)::crlfcrlf_nonascii_searcher_t, (anonymous namespace)::asio_server_connection::start_request_response()::{lambda(boost::system::error_code const&, unsigned long)#2}>::operator()(boost::system::error_code const&, unsigned long, int)
[ 10 ] boost::asio::detail::reactive_socket_recv_op<boost::asio::mutable_buffers_1, boost::asio::detail::read_until_match_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp>, boost::asio::basic_streambuf_ref<std::allocator<char> >, (anonymous namespace)::crlfcrlf_nonascii_searcher_t, (anonymous namespace)::asio_server_connection::start_request_response()::{lambda(boost::system::error_code const&, unsigned long)#2}> >::do_complete(void*, boost::asio::detail::scheduler_operation*, boost::system::error_code const&, unsigned long)
[ 11 ] (anonymous namespace)::threadpool_impl::thread_start(void*)
[ 12 ] start_thread
[ 13 ] __clone

The exception is from https://github.com/boostorg/asio/blob/develop/include/boost/asio/basic_socket.hpp#L1625

I don't understand the reason that throws the exception here, can someone help to take a look? Additionally, can we make a fix to catch the exception and gracefully return the error instead of crashing the whole progrm? Thanks!

SiyiJia commented 2 years ago

I see there is a similar one https://github.com/boostorg/asio/issues/328 raised before.

bazineta commented 2 years ago

Likely what you're seeing here is that the other end dropped the connection (or some intervening device did; the effect is the same) between the time that you accepted the connection and when you called remote_endpoint(). No matter how small that window of time is, that's what may have occurred.

That being the case, your two options here are to wrap that call in a try...catch, or to use the non-throwing form of the call, where you supply an error_code to the call, e.g., socket.remote_endpoint(ec). That form will return a default-constructed endpoint in the case of error.