Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions distros/jazzy/patch/ros2-class-loader.patch
Original file line number Diff line number Diff line change
@@ -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 <cstddef>
#include <mutex>
#include <string>
@@ -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();
}
};

2 changes: 2 additions & 0 deletions distros/jazzy/pkg_additional_info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 23 additions & 13 deletions distros/lyrical/patch/ros2-class-loader.patch
Original file line number Diff line number Diff line change
@@ -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<class Base>
std::vector<std::string> 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 <cstddef>
#include <mutex>
#include <string>
@@ -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();
}
};

2 changes: 2 additions & 0 deletions distros/lyrical/pkg_additional_info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
72 changes: 59 additions & 13 deletions distros/rolling/patch/ros2-class-loader.patch
Original file line number Diff line number Diff line change
@@ -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<class Base>
std::vector<std::string> 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 <algorithm>
#include <cstddef>
#include <memory>
@@ -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();
}
};

2 changes: 2 additions & 0 deletions distros/rolling/pkg_additional_info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 28 additions & 0 deletions shared/tests/ros2-class-loader.yaml
Original file line number Diff line number Diff line change
@@ -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/
14 changes: 14 additions & 0 deletions shared/tests/ros2-class-loader/exit_order/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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="$<TARGET_FILE:exit_order_plugin>")
add_dependencies(exit_order exit_order_plugin)
11 changes: 11 additions & 0 deletions shared/tests/ros2-class-loader/exit_order/base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef BASE_HPP_
#define BASE_HPP_

class Base
{
public:
virtual ~Base() = default;
virtual int value() const = 0;
};

#endif // BASE_HPP_
30 changes: 30 additions & 0 deletions shared/tests/ros2-class-loader/exit_order/main.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdio>
#include <memory>

#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<Base>("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;
}
11 changes: 11 additions & 0 deletions shared/tests/ros2-class-loader/exit_order/plugin.cpp
Original file line number Diff line number Diff line change
@@ -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)
Loading