Reputation: 1795
I have a simple piece of boost cobalt+asio code here to resolve a URL to an endpoint.
#include <boost/cobalt.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <iostream>
#include <string>
#include <vector>
namespace net = boost::asio;
namespace cobalt = boost::cobalt;
using executor_type = cobalt::executor;
cobalt::task<std::vector<net::ip::tcp::endpoint>> resolve(
executor_type executor,
std::string const& hostname,
std::string const& service
)
{
auto endpoints = std::vector<net::ip::tcp::endpoint>{};
auto resolver = net::ip::tcp::resolver{executor};
try
{
auto results = co_await resolver.async_resolve(
hostname,
service,
cobalt::use_op);
for (const auto& result : results)
endpoints.push_back(result.endpoint());
co_return endpoints;
}
catch (...)
{
std::cerr << "Resolve exception\n";
throw;
}
}
cobalt::task<void> main2()
{
auto executor = co_await cobalt::this_coro::executor;
auto endpoints = co_await resolve(executor, "www.googleqqq.com", "https"); // Intentionally wrong; use www.google.com to make it work
// Print the resolved endpoints
for (const auto& endpoint : endpoints)
std::cerr << endpoint << std::endl;
co_return;
}
int main()
{
try
{
net::io_context ctx;
cobalt::spawn(ctx, main2(), net::detached);
ctx.run();
}
catch (...)
{
std::cerr << "Uncaught exception\n";
}
return 0;
}
And the CMakeLists.txt:
cmake_minimum_required(VERSION 3.30)
project(async_resolve_exception_issue)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(FetchContent)
# OpenSSL
find_package(OpenSSL REQUIRED)
# Boost
FetchContent_Declare(
boost
GIT_REPOSITORY https://github.com/boostorg/boost.git
GIT_TAG boost-1.85.0
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(boost)
find_package(Boost 1.85 REQUIRED COMPONENTS system)
add_executable(${PROJECT_NAME}
${PROJECT_NAME}.cpp
)
target_link_libraries(${PROJECT_NAME}
PUBLIC
Boost::beast
Boost::cobalt
OpenSSL::SSL
OpenSSL::Crypto
)
I invoke with:
build/async_resolve_exception_issue
And the error message is:
libc++abi: terminating due to uncaught exception of type boost::detail::with_throw_location<boost::system::system_error>: Host not found (authoritative) [asio.netdb:1]
I am using MacOS Sonoma (14.6) + cmake 3.30.2 + clang 18.1.8
Upvotes: 1
Views: 50