Skip to content
Open
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
3 changes: 3 additions & 0 deletions include/class_loader/class_loader_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/class_loader_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,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()
{
Expand Down
7 changes: 7 additions & 0 deletions src/multi_library_class_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <string>
#include <vector>

#include "console_bridge/console.h"

namespace class_loader
{

Expand All @@ -57,6 +59,11 @@ class ClassLoaderDependency
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();
}
};

Expand Down
9 changes: 9 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,13 @@ if(TARGET ${PROJECT_NAME}_external_load_test)
${PROJECT_NAME}_TestPlugins2)
endif()

ament_add_gtest(${PROJECT_NAME}_exit_order_test exit_order_test.cpp
APPEND_LIBRARY_DIRS "${append_library_dirs}"
)
if(TARGET ${PROJECT_NAME}_exit_order_test)
target_include_directories(${PROJECT_NAME}_exit_order_test PUBLIC "../include")
target_link_libraries(${PROJECT_NAME}_exit_order_test ${PROJECT_NAME})
add_dependencies(${PROJECT_NAME}_exit_order_test ${PROJECT_NAME}_TestPlugins1)
endif()

add_subdirectory(fviz_case_study)
57 changes: 57 additions & 0 deletions test/exit_order_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2026, Open Source Robotics Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <cstdlib>
#include <string>

#include "class_loader/multi_library_class_loader.hpp"

#include "gtest/gtest.h"

#include "./base.hpp"

// A MultiLibraryClassLoader that is never destroyed (plugins held until the process ends)
// leaves its ClassLoader in the static ClassLoaderPtrVectorImpl, which is destroyed at exit.
// ~ClassLoader() logs through console_bridge and unloads under the loader mutex, so both of
// those statics must outlive it. This runs in its own executable so that none of these
// statics exist before the test, as at the start of a real process.

const std::string LIBRARY_1 = class_loader::systemLibraryFormat("class_loader_TestPlugins1"); // NOLINT

TEST(ClassLoaderExitOrderTest, exitWithLiveMultiLibraryClassLoader) {
EXPECT_EXIT(
{
auto * loader = new class_loader::MultiLibraryClassLoader(false); // never destroyed
loader->loadLibrary(LIBRARY_1);
loader->createUniqueInstance<Base>("Dog")->saySomething();
std::exit(0); // runs the static destructors
}, ::testing::ExitedWithCode(0), "");
}