diff --git a/distros/jazzy/patch/ros2-class-loader.patch b/distros/jazzy/patch/ros2-class-loader.patch new file mode 100644 index 000000000..011a2eb3f --- /dev/null +++ b/distros/jazzy/patch/ros2-class-loader.patch @@ -0,0 +1,23 @@ +diff --git a/src/multi_library_class_loader.cpp b/src/multi_library_class_loader.cpp +index d7ba907..f315001 100644 +--- a/src/multi_library_class_loader.cpp ++++ b/src/multi_library_class_loader.cpp +@@ -29,6 +29,8 @@ + + #include "class_loader/multi_library_class_loader.hpp" + ++#include "console_bridge/console.h" ++ + #include + #include + #include +@@ -55,6 +57,9 @@ protected: + class_loader::impl::getCurrentlyLoadingLibraryName(); + class_loader::impl::getCurrentlyActiveClassLoader(); + class_loader::impl::hasANonPurePluginLibraryBeenOpened(); ++ // ~ClassLoader() logs through console_bridge, so its output handler (a function-local ++ // static in console_bridge) has to outlive `class_loader_ptrs_` as well ++ console_bridge::getLogLevel(); + } + }; + diff --git a/distros/jazzy/pkg_additional_info.yaml b/distros/jazzy/pkg_additional_info.yaml index cc35392a8..636b844f7 100644 --- a/distros/jazzy/pkg_additional_info.yaml +++ b/distros/jazzy/pkg_additional_info.yaml @@ -156,5 +156,7 @@ zenoh_cpp_vendor: cartographer_ros: additional_cmake_args: "-DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=ON" build_number: 32 # offline_node.cpp out of the shared library (duplicate gflags abort) +class_loader: # console_bridge outlives the class loaders (exit abort on macOS) + build_number: 32 rmf_utils: # its DLL moved from lib to bin (Windows) build_number: 32 diff --git a/distros/lyrical/patch/ros2-class-loader.patch b/distros/lyrical/patch/ros2-class-loader.patch index 9ca4d5b3b..0ece11e2a 100644 --- a/distros/lyrical/patch/ros2-class-loader.patch +++ b/distros/lyrical/patch/ros2-class-loader.patch @@ -1,13 +1,23 @@ -diff --git a/include/class_loader/multi_library_class_loader.hpp b/include/class_loader/multi_library_class_loader.hpp -index 1f17e47..4117e55 100644 ---- a/include/class_loader/multi_library_class_loader.hpp -+++ b/include/class_loader/multi_library_class_loader.hpp -@@ -296,7 +296,7 @@ public: - template - std::vector getAvailableClassesForLibrary(const std::string & library_path) const - { -- ClassLoader * loader = getClassLoaderForLibrary(library_path); -+ const ClassLoader * loader = getClassLoaderForLibrary(library_path); - if (nullptr == loader) { - throw class_loader::NoClassLoaderExistsException( - "There is no ClassLoader in MultiLibraryClassLoader bound to library " + +diff --git a/src/multi_library_class_loader.cpp b/src/multi_library_class_loader.cpp +index 7ff7d09..335fb97 100644 +--- a/src/multi_library_class_loader.cpp ++++ b/src/multi_library_class_loader.cpp +@@ -29,6 +29,8 @@ + + #include "class_loader/multi_library_class_loader.hpp" + ++#include "console_bridge/console.h" ++ + #include + #include + #include +@@ -55,6 +57,9 @@ protected: + class_loader::impl::getCurrentlyLoadingLibraryName(); + class_loader::impl::getCurrentlyActiveClassLoader(); + class_loader::impl::hasANonPurePluginLibraryBeenOpened(); ++ // ~ClassLoader() logs through console_bridge, so its output handler (a function-local ++ // static in console_bridge) has to outlive `class_loader_ptrs_` as well ++ console_bridge::getLogLevel(); + } + }; + diff --git a/distros/lyrical/pkg_additional_info.yaml b/distros/lyrical/pkg_additional_info.yaml index cf261f400..ba69464e9 100644 --- a/distros/lyrical/pkg_additional_info.yaml +++ b/distros/lyrical/pkg_additional_info.yaml @@ -116,6 +116,8 @@ aruco_ros: # its recipe test stops the node for sure (package tests hung on mac cartographer_ros: additional_cmake_args: "-DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=ON -DCMAKE_CXX_FLAGS=-DBOOST_ZLIB_BINARY=kernel32" build_number: 31 # offline_node.cpp out of the shared library (duplicate gflags abort) +class_loader: # console_bridge outlives the class loaders (exit abort on macOS) + build_number: 31 fuse_viz: build_number: 31 grid_map_rviz_plugin: diff --git a/distros/rolling/patch/ros2-class-loader.patch b/distros/rolling/patch/ros2-class-loader.patch index 9ca4d5b3b..a07b74c47 100644 --- a/distros/rolling/patch/ros2-class-loader.patch +++ b/distros/rolling/patch/ros2-class-loader.patch @@ -1,13 +1,59 @@ -diff --git a/include/class_loader/multi_library_class_loader.hpp b/include/class_loader/multi_library_class_loader.hpp -index 1f17e47..4117e55 100644 ---- a/include/class_loader/multi_library_class_loader.hpp -+++ b/include/class_loader/multi_library_class_loader.hpp -@@ -296,7 +296,7 @@ public: - template - std::vector getAvailableClassesForLibrary(const std::string & library_path) const - { -- ClassLoader * loader = getClassLoaderForLibrary(library_path); -+ const ClassLoader * loader = getClassLoaderForLibrary(library_path); - if (nullptr == loader) { - throw class_loader::NoClassLoaderExistsException( - "There is no ClassLoader in MultiLibraryClassLoader bound to library " + +diff --git a/include/class_loader/class_loader_core.hpp b/include/class_loader/class_loader_core.hpp +index 418418b..2fb1f32 100644 +--- a/include/class_loader/class_loader_core.hpp ++++ b/include/class_loader/class_loader_core.hpp +@@ -201,6 +201,9 @@ CLASS_LOADER_PUBLIC + std::recursive_mutex & getLoadedLibraryVectorMutex(); + CLASS_LOADER_PUBLIC + std::recursive_mutex & getPluginBaseToFactoryMapMapMutex(); ++/// Serializes loadLibrary()/unloadLibrary() so a dlopen() never overlaps a dlclose(). ++CLASS_LOADER_PUBLIC ++std::recursive_mutex & getLoaderMutex(); + + /** + * @brief Indicates if a library containing more than just plugins has been opened by the running process +diff --git a/src/class_loader_core.cpp b/src/class_loader_core.cpp +index 55c475d..d7cf07e 100644 +--- a/src/class_loader_core.cpp ++++ b/src/class_loader_core.cpp +@@ -56,15 +56,12 @@ std::recursive_mutex & getPluginBaseToFactoryMapMapMutex() + return m; + } + +-namespace +-{ + // Serializes loadLibrary()/unloadLibrary() so a dlopen() never overlaps a dlclose(). + std::recursive_mutex & getLoaderMutex() + { + static std::recursive_mutex m; + return m; + } +-} // namespace + + BaseToFactoryMapMap & getGlobalPluginBaseToFactoryMapMap() + { +diff --git a/src/multi_library_class_loader.cpp b/src/multi_library_class_loader.cpp +index 36f4aa0..03ee91c 100644 +--- a/src/multi_library_class_loader.cpp ++++ b/src/multi_library_class_loader.cpp +@@ -29,6 +29,8 @@ + + #include "class_loader/multi_library_class_loader.hpp" + ++#include "console_bridge/console.h" ++ + #include + #include + #include +@@ -57,6 +59,11 @@ protected: + class_loader::impl::getCurrentlyLoadingLibraryName(); + class_loader::impl::getCurrentlyActiveClassLoader(); + class_loader::impl::hasANonPurePluginLibraryBeenOpened(); ++ // ~ClassLoader() unloads its library under the loader mutex ++ class_loader::impl::getLoaderMutex(); ++ // ~ClassLoader() logs through console_bridge, so its output handler (a function-local ++ // static in console_bridge) has to outlive `class_loader_ptrs_` as well ++ console_bridge::getLogLevel(); + } + }; + diff --git a/distros/rolling/pkg_additional_info.yaml b/distros/rolling/pkg_additional_info.yaml index ff6c5e9c7..bad25f256 100644 --- a/distros/rolling/pkg_additional_info.yaml +++ b/distros/rolling/pkg_additional_info.yaml @@ -74,6 +74,8 @@ aruco_ros: # its recipe test stops the node for sure (package tests hung on mac cartographer_ros: additional_cmake_args: "-DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=ON -DCMAKE_CXX_FLAGS=-DBOOST_ZLIB_BINARY=kernel32" build_number: 33 # offline_node.cpp out of the shared library (duplicate gflags abort) +class_loader: # console_bridge outlives the class loaders (exit abort on macOS) + build_number: 33 fuse_viz: build_number: 33 moveit_ros_visualization: diff --git a/shared/tests/ros2-class-loader.yaml b/shared/tests/ros2-class-loader.yaml new file mode 100644 index 000000000..0afb7fbd3 --- /dev/null +++ b/shared/tests/ros2-class-loader.yaml @@ -0,0 +1,28 @@ +tests: + # Regression test (https://github.com/RoboStack/robostack/pull/243, ros/class_loader#252): + # a process that exits while a MultiLibraryClassLoader still holds a library must exit + # cleanly. When console_bridge's output handler was destroyed before class_loader's + # loaders, ~ClassLoader() logged through it and the process aborted ("mutex lock failed", + # exit code 134). Only macOS's libc++ throws on the destroyed mutex; elsewhere the test + # just checks that loading a plugin and exiting works. + - if: unix + then: + script: + # Python hints as in ros2-ament-cmake-gmock: ament_cmake runs Python helpers, which must + # not pick up the system Python + - >- + cmake -S exit_order -B build -G Ninja -DCMAKE_PREFIX_PATH="$PREFIX" + -DPYTHON_EXECUTABLE="$PREFIX/bin/python" -DPython_EXECUTABLE="$PREFIX/bin/python" + -DPython3_EXECUTABLE="$PREFIX/bin/python" -DPython3_FIND_STRATEGY=LOCATION + - cmake --build build + - ./build/exit_order + requirements: + build: + - cxx-compiler + - cmake + - ninja + run: + - ros2-ament-cmake # class_loader's CMake config finds its exported dependencies with it + files: + recipe: + - exit_order/ diff --git a/shared/tests/ros2-class-loader/exit_order/CMakeLists.txt b/shared/tests/ros2-class-loader/exit_order/CMakeLists.txt new file mode 100644 index 000000000..bba35a19c --- /dev/null +++ b/shared/tests/ros2-class-loader/exit_order/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.20) +project(class_loader_exit_order CXX) + +set(CMAKE_CXX_STANDARD 17) +find_package(class_loader REQUIRED) + +add_library(exit_order_plugin SHARED plugin.cpp) +target_link_libraries(exit_order_plugin class_loader::class_loader) + +add_executable(exit_order main.cpp) +target_link_libraries(exit_order class_loader::class_loader) +target_compile_definitions(exit_order PRIVATE + PLUGIN_PATH="$") +add_dependencies(exit_order exit_order_plugin) diff --git a/shared/tests/ros2-class-loader/exit_order/base.hpp b/shared/tests/ros2-class-loader/exit_order/base.hpp new file mode 100644 index 000000000..7a79e889d --- /dev/null +++ b/shared/tests/ros2-class-loader/exit_order/base.hpp @@ -0,0 +1,11 @@ +#ifndef BASE_HPP_ +#define BASE_HPP_ + +class Base +{ +public: + virtual ~Base() = default; + virtual int value() const = 0; +}; + +#endif // BASE_HPP_ diff --git a/shared/tests/ros2-class-loader/exit_order/main.cpp b/shared/tests/ros2-class-loader/exit_order/main.cpp new file mode 100644 index 000000000..c823a0b35 --- /dev/null +++ b/shared/tests/ros2-class-loader/exit_order/main.cpp @@ -0,0 +1,30 @@ +// Regression test for the exit order of class_loader's statics and console_bridge's +// (ros/class_loader#252, RoboStack/robostack#243). +// +// A MultiLibraryClassLoader that is never destroyed (as with plugins held until the +// process ends, e.g. rqt) leaves its ClassLoader in class_loader's static +// ClassLoaderPtrVectorImpl. That static is created right before the ClassLoader's first +// console_bridge log, so without the fix console_bridge's output handler is destroyed +// first at exit, ~ClassLoader() then logs through it: "mutex lock failed", +// std::terminate (abort, exit code 134 on macOS). The test passes if the process exits 0. +#include +#include + +#include "class_loader/multi_library_class_loader.hpp" + +#include "base.hpp" + +int main() +{ + auto * loader = new class_loader::MultiLibraryClassLoader(false); // never destroyed + loader->loadLibrary(PLUGIN_PATH); + { + auto instance = loader->createUniqueInstance("Derived"); + if (instance->value() != 42) { + std::fprintf(stderr, "unexpected plugin value\n"); + return 1; + } + } + std::printf("loaded %s; exiting with the class loader still alive\n", PLUGIN_PATH); + return 0; +} diff --git a/shared/tests/ros2-class-loader/exit_order/plugin.cpp b/shared/tests/ros2-class-loader/exit_order/plugin.cpp new file mode 100644 index 000000000..02cb605b6 --- /dev/null +++ b/shared/tests/ros2-class-loader/exit_order/plugin.cpp @@ -0,0 +1,11 @@ +#include "class_loader/class_loader.hpp" + +#include "base.hpp" + +class Derived : public Base +{ +public: + int value() const override {return 42;} +}; + +CLASS_LOADER_REGISTER_CLASS(Derived, Base)