From 9ba3afdde1967110e8c511b7b96682893bba9505 Mon Sep 17 00:00:00 2001 From: Hartmut Kaiser Date: Sun, 22 Nov 2026 10:25:26 -0600 Subject: [PATCH 1/2] Show the error code once in errors that embed another error std::system_error appends the message of the error code to the message of an hpx::exception (": HPX(network_error)"). An error whose message embeds the message of another error with the same code, e.g. the error that caused it, showed the code twice. The code embedded in the message is now removed, it is appended once. An embedded error with a different code keeps its code, and messages of rethrown errors are left as they are. Signed-off-by: Hartmut Kaiser --- libs/core/errors/src/exception.cpp | 32 +++++++++++++++++++-- libs/core/errors/tests/unit/exception.cpp | 35 +++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/libs/core/errors/src/exception.cpp b/libs/core/errors/src/exception.cpp index b526e2acbf42..21aeca811d9c 100644 --- a/libs/core/errors/src/exception.cpp +++ b/libs/core/errors/src/exception.cpp @@ -34,6 +34,32 @@ namespace hpx { + namespace { + + // std::system_error appends the message of the error code to the + // message of the exception (": HPX(network_error)"). A message that + // embeds the message of another error with the same code (e.g. the + // error that caused this one) would show it twice: remove it from + // the message, it is appended once. Codes that add no text (rethrow) + // are left alone. + std::string without_code(std::string msg, std::error_code const& ec) + { + std::string const code = ec.message(); + if (code.empty()) + { + return msg; + } + + std::string const suffix = ": " + code; + for (auto pos = msg.find(suffix); pos != std::string::npos; + pos = msg.find(suffix, pos)) + { + msg.erase(pos, suffix.size()); + } + return msg; + } + } // namespace + /////////////////////////////////////////////////////////////////////////// /// Construct a hpx::exception from a \a hpx::error. /// @@ -80,7 +106,8 @@ namespace hpx { /// is the default) or to the category \a hpx_category_rethrow /// (if mode is \a throwmode::rethrow). exception::exception(hpx::error e, char const* msg, throwmode mode) - : std::system_error(make_system_error_code(e, mode), msg) + : std::system_error(make_system_error_code(e, mode), + without_code(msg, make_system_error_code(e, mode))) { HPX_ASSERT((e >= hpx::error::success && e < hpx::error::last_error) || (e & hpx::error::system_error_flag)); @@ -103,7 +130,8 @@ namespace hpx { /// the default) or to the category \a hpx_category_rethrow /// (if mode is \a throwmode::rethrow). exception::exception(hpx::error e, std::string const& msg, throwmode mode) - : std::system_error(make_system_error_code(e, mode), msg) + : std::system_error(make_system_error_code(e, mode), + without_code(msg, make_system_error_code(e, mode))) { HPX_ASSERT((e >= hpx::error::success && e < hpx::error::last_error) || (e & hpx::error::system_error_flag)); diff --git a/libs/core/errors/tests/unit/exception.cpp b/libs/core/errors/tests/unit/exception.cpp index 12bfa98bd2d6..eac6759dca82 100644 --- a/libs/core/errors/tests/unit/exception.cpp +++ b/libs/core/errors/tests/unit/exception.cpp @@ -98,5 +98,40 @@ int main() HPX_TEST_EQ(hpx::get_error_function_name(ptr), "throw_always"); } + // an error embedding the message of another one with the same code shows + // the code once, a different code is kept + { + std::exception_ptr const inner = test_transport(); + + exception_caught = false; + try + { + HPX_THROW_EXCEPTION(hpx::error::no_success, "wrap_same", + "wrapped: {}", hpx::get_error_what(inner)); + } + catch (hpx::exception const& e) + { + exception_caught = true; + HPX_TEST_EQ(hpx::get_error_what(e), + "wrapped: simulated error: HPX(no_success)"); + } + HPX_TEST(exception_caught); + + exception_caught = false; + try + { + HPX_THROW_EXCEPTION(hpx::error::bad_parameter, "wrap_other", + "wrapped: {}", hpx::get_error_what(inner)); + } + catch (hpx::exception const& e) + { + exception_caught = true; + HPX_TEST_EQ(hpx::get_error_what(e), + "wrapped: simulated error: HPX(no_success): " + "HPX(bad_parameter)"); + } + HPX_TEST(exception_caught); + } + return hpx::util::report_errors(); } From d5110a825154480b41617b7fd7efffc370f1cb99 Mon Sep 17 00:00:00 2001 From: Hartmut Kaiser Date: Sun, 22 Nov 2026 10:25:27 -0600 Subject: [PATCH 2/2] Report parcelport failures at startup instead of terminating Errors thrown while hpx::init creates the runtime (which creates the parcelports and the device conduit, and connects the localities) escaped main, and a bootstrap parcelport that failed to start, or none being usable, called std::terminate: HPX aborted with a core dump. hpx::init now reports these errors like its other startup errors and returns -1. A bootstrap parcelport that fails to start or to initialize, no usable bootstrap parcelport, and all parcelports failing are reported as errors, after the parcelports that run are stopped (joining their I/O threads). The unwinding exposed two failures: destroy_big_boot_barrier threw from ~addressing_service when there was no barrier yet, and ~runtime waited on thread pools that never ran. The former does nothing then, the latter waits only for pools that ran. Signed-off-by: Hartmut Kaiser --- libs/core/runtime_local/src/runtime_local.cpp | 11 ++- libs/full/init_runtime/src/hpx_init.cpp | 71 ++++++++++------ libs/full/parcelset/src/parcelhandler.cpp | 77 +++++++++++++----- .../src/big_boot_barrier.cpp | 8 +- .../src/runtime_distributed.cpp | 80 ++++++++++++------- 5 files changed, 170 insertions(+), 77 deletions(-) diff --git a/libs/core/runtime_local/src/runtime_local.cpp b/libs/core/runtime_local/src/runtime_local.cpp index 53b0de89ee23..b8d34a4c5936 100644 --- a/libs/core/runtime_local/src/runtime_local.cpp +++ b/libs/core/runtime_local/src/runtime_local.cpp @@ -561,6 +561,12 @@ namespace hpx { { LRT_(debug).format("~runtime_local(entering)"); + // The thread pools never ran if the construction of the runtime + // failed (for instance, a parcelport couldn't start): their + // schedulers aren't set up for waiting then. + bool const threads_ran = + thread_manager_->status() != hpx::state::initialized; + // stop all services thread_manager_->stop(); // stops timer_pool_ as well #ifdef HPX_HAVE_IO_POOL @@ -571,7 +577,10 @@ namespace hpx { // This ensures that collective operations and other background work // finish before reinit_destruct() destroys component heaps. // Fixes issue #6776: crash in one_size_heap_list::alloc on macOS Debug - thread_manager_->wait(); + if (threads_ran) + { + thread_manager_->wait(); + } LRT_(debug).format("~runtime_local(finished)"); diff --git a/libs/full/init_runtime/src/hpx_init.cpp b/libs/full/init_runtime/src/hpx_init.cpp index 9fbdd59eccdc..01723919a28c 100644 --- a/libs/full/init_runtime/src/hpx_init.cpp +++ b/libs/full/init_runtime/src/hpx_init.cpp @@ -962,35 +962,60 @@ namespace hpx { // Command line handling should have updated this by now. HPX_ASSERT(cmdline.rtcfg_.mode_ != runtime_mode::default_); - if (cmdline.rtcfg_.mode_ == runtime_mode::local) - { - LPROGRESS_ << "creating local runtime"; - rt.reset(new hpx::runtime(cmdline.rtcfg_, true)); - } - else + + // The runtime's constructor creates and starts the + // parcelports and bootstraps the localities: errors there + // (invalid settings, a parcelport that can't start) are + // reported like the other errors during startup, instead of + // leaving hpx::init. Its thread pools haven't started yet. + try { -#if defined(HPX_HAVE_DISTRIBUTED_RUNTIME) - for (auto const& registry : component_registries) + if (cmdline.rtcfg_.mode_ == runtime_mode::local) { - hpx::register_startup_function([registry]() { - registry->register_component_type(); - }); + LPROGRESS_ << "creating local runtime"; + rt.reset(new hpx::runtime(cmdline.rtcfg_, true)); } + else + { +#if defined(HPX_HAVE_DISTRIBUTED_RUNTIME) + for (auto const& registry : component_registries) + { + hpx::register_startup_function([registry]() { + registry->register_component_type(); + }); + } - LPROGRESS_ << "creating distributed runtime"; - rt.reset(new hpx::runtime_distributed(cmdline.rtcfg_, - &hpx::detail::pre_main, &hpx::detail::post_main)); + LPROGRESS_ << "creating distributed runtime"; + rt.reset(new hpx::runtime_distributed(cmdline.rtcfg_, + &hpx::detail::pre_main, &hpx::detail::post_main)); #else - HPX_THROW_EXCEPTION(hpx::error::invalid_status, - "run_or_start", - "Attempted to start the runtime in the mode \"{1}\", " - "but HPX was compiled with " - "HPX_WITH_DISTRIBUTED_RUNTIME=OFF, and \"{1}\" " - "requires HPX_WITH_DISTRIBUTED_RUNTIME=ON. " - "Recompile HPX with HPX_WITH_DISTRIBUTED_RUNTIME=ON or " - "change the runtime mode.", - get_runtime_mode_name(cmdline.rtcfg_.mode_)); + HPX_THROW_EXCEPTION(hpx::error::invalid_status, + "run_or_start", + "Attempted to start the runtime in the mode " + "\"{1}\", but HPX was compiled with " + "HPX_WITH_DISTRIBUTED_RUNTIME=OFF, and \"{1}\" " + "requires HPX_WITH_DISTRIBUTED_RUNTIME=ON. " + "Recompile HPX with " + "HPX_WITH_DISTRIBUTED_RUNTIME=ON " + "or change the runtime mode.", + get_runtime_mode_name(cmdline.rtcfg_.mode_)); #endif + } + } + catch (hpx::exception const& e) + { + resource::detail::delete_partitioner(); + std::cerr << "hpx::init: hpx::exception caught: " + << hpx::get_error_what(e) << "\n"; + return -1; + } + catch (std::exception const& e) + { + resource::detail::delete_partitioner(); + std::cerr + << "hpx::init: std::exception caught: " << e.what() + << "\n"; + return -1; } // Store application defined command line options diff --git a/libs/full/parcelset/src/parcelhandler.cpp b/libs/full/parcelset/src/parcelhandler.cpp index e897fc188f93..b5afc408d6c9 100644 --- a/libs/full/parcelset/src/parcelhandler.cpp +++ b/libs/full/parcelset/src/parcelhandler.cpp @@ -265,30 +265,31 @@ namespace hpx::parcelset { if (hpx::is_networking_enabled()) { - std::cerr << "Could not find usable bootstrap parcelport.\n"; - std::cerr << hpx::util::format( - "Preconfigured bootstrap parcelport: '{}'\n", + // this locality can't start: the runtime's construction fails, + // and hpx::init reports the error + std::ostringstream strm; + hpx::util::format_to(strm, + "could not find a usable bootstrap parcelport (configured " + "bootstrap parcelport: '{}'), ", get_config_entry(cfgkey, "tcp")); - if (pports_.empty()) { - std::cerr << "No available parcelports\n"; + strm << "no parcelports are available"; } else { - std::cerr << "List of available parcelports:\n"; + strm << "available parcelports:"; for (auto const& pp : pports_) { - std::cerr << hpx::util::format( - " {}, priority: {}, can bootstrap: {}\n", + hpx::util::format_to(strm, + "\n {}, priority: {}, can bootstrap: {}", pp.second->type(), pp.first, pp.second->can_bootstrap()); } - std::cerr << "\n"; } - - // terminate this locality as there is nothing else we can do - std::terminate(); + HPX_THROW_EXCEPTION(hpx::error::network_error, + "hpx::parcelset::parcelhandler::get_bootstrap_parcelport", "{}", + strm.str()); } return {}; @@ -298,13 +299,20 @@ namespace hpx::parcelset { { exception_list exceptions; std::vector failed_pps; + + // initialize_agas has started the bootstrap parcelport already (it + // failed there if there is none) + std::shared_ptr const bootstrap = + get_bootstrap_parcelport(); + bool bootstrap_failed = false; + for (pports_type::value_type& pp : pports_) { // protect against exceptions thrown by a parcelport during // initialization hpx::detail::try_catch_exception_ptr( [&]() { - if (pp.second != get_bootstrap_parcelport()) + if (pp.second != bootstrap) { if (pp.first > 0) pp.second->run(false); @@ -320,19 +328,50 @@ namespace hpx::parcelset { [&](std::exception_ptr&& e) { exceptions.add(HPX_MOVE(e)); failed_pps.push_back(pp.first); + bootstrap_failed = + bootstrap_failed || pp.second == bootstrap; }); } // handle exceptions if (exceptions.size() != 0) { - if (failed_pps.size() == pports_.size()) + if (failed_pps.size() == pports_.size() || bootstrap_failed) { - std::cerr << hpx::util::format( - "all parcelports failed initializing on locality {}, " - "exiting:\n{}\n", - agas::get_locality_id(), exceptions.get_message()); - std::terminate(); + // this locality can't communicate (with the bootstrap + // parcelport, AGAS and the other localities can't reach it): + // stop the parcelports (joining their I/O threads), the + // runtime's construction fails, and hpx::init reports the + // errors + for (auto const& pp : pports_) + { + if (pp.first <= 0) + { + continue; + } + try + { + pp.second->stop(); + } + // NOLINTNEXTLINE(bugprone-empty-catch) + catch (...) + { + } + } + if (failed_pps.size() == pports_.size()) + { + HPX_THROW_EXCEPTION(hpx::error::network_error, + "hpx::parcelset::parcelhandler::initialize", + "all parcelports failed initializing on locality " + "{}:\n{}", + agas::get_locality_id(), exceptions.get_message()); + } + HPX_THROW_EXCEPTION(hpx::error::network_error, + "hpx::parcelset::parcelhandler::initialize", + "the bootstrap parcelport ({}) failed initializing on " + "locality {}:\n{}", + bootstrap->type(), agas::get_locality_id(), + exceptions.get_message()); } else { diff --git a/libs/full/runtime_distributed/src/big_boot_barrier.cpp b/libs/full/runtime_distributed/src/big_boot_barrier.cpp index aa6002978912..e6393cbb9f14 100644 --- a/libs/full/runtime_distributed/src/big_boot_barrier.cpp +++ b/libs/full/runtime_distributed/src/big_boot_barrier.cpp @@ -847,12 +847,8 @@ namespace hpx::agas { { util::reinitializable_static, bbb_tag> bbb; - if (!bbb.get()) - { - HPX_THROW_EXCEPTION(hpx::error::internal_server_error, - "destroy_big_boot_barrier", - "big_boot_barrier has not been created yet"); - } + // called from ~addressing_service, also when the runtime's + // construction failed before the barrier was created bbb.get().reset(); } diff --git a/libs/full/runtime_distributed/src/runtime_distributed.cpp b/libs/full/runtime_distributed/src/runtime_distributed.cpp index a3807ef7ccba..7dcc78d2175d 100644 --- a/libs/full/runtime_distributed/src/runtime_distributed.cpp +++ b/libs/full/runtime_distributed/src/runtime_distributed.cpp @@ -231,6 +231,44 @@ namespace hpx { reinterpret_cast(runtime_support_.get())); } +#if defined(HPX_HAVE_NETWORKING) + namespace { + + // Starts the bootstrap parcelport. One that fails is stopped, which + // joins the I/O threads run() may have started, and its error is + // rethrown: the construction of the runtime fails, and hpx::init + // reports the error. + void run_bootstrap_parcelport(parcelset::parcelport& pp) + { + std::exception_ptr error; + try + { + pp.run(false); + return; + } + catch (...) + { + error = std::current_exception(); + } + + // the parcelport's own error is the one reported + try + { + pp.stop(true); + } + // NOLINTNEXTLINE(bugprone-empty-catch) + catch (...) + { + } + + HPX_THROW_EXCEPTION(hpx::error::network_error, + "hpx::runtime_distributed::initialize_agas", + "the bootstrap parcelport ({}) has failed to start: {}", + pp.type(), hpx::get_error_what(error)); + } + } // namespace +#endif + void runtime_distributed::initialize_agas() { #if defined(HPX_HAVE_NETWORKING) @@ -261,20 +299,8 @@ namespace hpx { parcelset::locality const published = pp ? pp->here() : parcelset::locality(); - hpx::detail::try_catch_exception_ptr( - [&]() { - if (pp) - pp->run(false); - }, - [&](std::exception_ptr const& e) { - std::cerr << hpx::util::format( - "the bootstrap parcelport ({}) has failed to " - "initialize on locality {}:\n{},\n" - "bailing out\n", - pp->type(), hpx::get_locality_id(), - hpx::get_error_what(e)); - std::terminate(); - }); + if (pp) + run_bootstrap_parcelport(*pp); // Binding may settle an endpoint that was left open in the // configuration, which every other locality is free to do. The @@ -287,6 +313,16 @@ namespace hpx { // condition and is left alone. if (pp && pp->here() != published) { + // the parcelport runs: stop it (joining its I/O threads) + // before the runtime's construction fails + try + { + pp->stop(true); + } + // NOLINTNEXTLINE(bugprone-empty-catch) + catch (...) + { + } HPX_THROW_EXCEPTION(hpx::error::network_error, "runtime_distributed::initialize_agas", "the AGAS root locality published {} and then bound {}; " @@ -300,20 +336,8 @@ namespace hpx { } else { - hpx::detail::try_catch_exception_ptr( - [&]() { - if (pp) - pp->run(false); - }, - [&](std::exception_ptr const& e) { - std::cerr << hpx::util::format( - "the bootstrap parcelport ({}) has failed to " - "initialize on locality {}:\n{},\n" - "bailing out\n", - pp->type(), hpx::get_locality_id(), - hpx::get_error_what(e)); - std::terminate(); - }); + if (pp) + run_bootstrap_parcelport(*pp); // The parcelport has bound by now, so its endpoint is final. It // was recorded when the parcelport was attached, which is before