Skip to content

Keep console_bridge's output handler and the loader mutex alive until the class loaders are destroyed - #252

Open
Tobias-Fischer wants to merge 2 commits into
ros:rollingfrom
Tobias-Fischer:fix-console-bridge-exit-order
Open

Tobias-Fischer wants to merge 2 commits into
ros:rollingfrom
Tobias-Fischer:fix-console-bridge-exit-order

Conversation

@Tobias-Fischer

@Tobias-Fischer Tobias-Fischer commented Nov 21, 2026 •

Copy link
Copy Markdown

On macOS every process that still holds class loaders through MultiLibraryClassLoader at exit aborts (seen with rqt, e.g. rqt --list-plugins exits with code 134):

std::terminate()
class_loader::ClassLoader::~ClassLoader()
class_loader::ClassLoaderPtrVectorImpl::~ClassLoaderPtrVectorImpl()
__cxa_finalize_ranges
exit

The exception is std::system_error: mutex lock failed, thrown from console_bridge::log (called by CONSOLE_BRIDGE_logDebug in ~ClassLoader()): console_bridge's output handler, a function-local static with a mutex, has already been destroyed when the static ClassLoaderPtrVectorImpl destroys the remaining loaders.

ClassLoaderDependency already constructs class_loader's own statics before class_loader_ptrs_ so that they are destroyed after it. This adds console_bridge::getLogLevel() there, which constructs console_bridge's output handler first, so it is destroyed after the loaders as well.

Loader mutex (rolling): getLoaderMutex(), added for concurrent load and unload, is another function-local static that ~ClassLoader() uses: unloadLibrary() locks it. It wasn't constructed in ClassLoaderDependency() either, so with only the console_bridge fix, the same exit path still aborted, this time with recursive_mutex lock failed. This PR moves it out of the anonymous namespace, declares it in class_loader_core.hpp next to the other impl mutex accessors, and constructs it in ClassLoaderDependency() too.

Regression test: test/exit_order_test.cpp is its own executable, so none of these statics exist before the test runs. Its death test exits while a MultiLibraryClassLoader still holds a library. On macOS (arm64), with rolling at 6aff570:

  • Without the fixes: it fails with mutex lock failed.
  • With them: it passes.
  • On Linux, locking a destroyed mutex doesn't throw, so there it only checks that a process can exit with a live loader.

Other testing:

  • class_loader_utest and class_loader_external_load_test pass.
  • class_loader_unmanaged_test crashes (SIGSEGV) on macOS with and without this PR, so it's unrelated.
  • The uncrustify divergence reported for test/base.hpp also predates this PR.
  • Earlier, with jazzy's class_loader 2.7.1 plus the console_bridge change, rqt --list-plugins exits with 0 instead of aborting.

🤖 Claude (AI assistant), on behalf of @Tobias-Fischer

🤖 Generated with Claude Code

…e destroyed

~ClassLoader() logs through CONSOLE_BRIDGE_logDebug. ClassLoaders still held by
MultiLibraryClassLoader live in the function-local static of
getClassLoaderPtrVectorImpl() and are destroyed at exit. On macOS console_bridge's
output handler (also a function-local static, with a mutex) can be destroyed before
that, so the log call locks a destroyed mutex, throws std::system_error from a
destructor and the process aborts on exit (e.g. every rqt exit).

ClassLoaderDependency already constructs class_loader's own statics first so that
they outlive class_loader_ptrs_; construct console_bridge's output handler there too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tobias Fischer <info@tobiasfischer.info>
…d a regression test

getLoaderMutex() (added with the concurrent load/unload fix) is another function-local
static that ~ClassLoader() uses: unloadLibrary() locks it. It wasn't constructed in
ClassLoaderDependency(), so it could be destroyed before class_loader_ptrs_ and
~ClassLoader() then locked a destroyed recursive_mutex at exit ("recursive_mutex lock
failed", std::terminate on macOS). Declare it in class_loader_core.hpp and construct it in
ClassLoaderDependency() like the other statics.

exit_order_test (own executable, so none of these statics exist beforehand) exits in a death
test while a MultiLibraryClassLoader still holds a library. Without these fixes it aborts on
macOS ("mutex lock failed").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tobias Fischer <info@tobiasfischer.info>
@Tobias-Fischer Tobias-Fischer changed the title Keep console_bridge's output handler alive until the class loaders are destroyed Keep console_bridge's output handler and the loader mutex alive until the class loaders are destroyed Nov 22, 2026
@Tobias-Fischer

Copy link
Copy Markdown
Author

I've pushed a second commit: the loader mutex fix and a regression test.

Writing the regression test exposed a second static with the same problem. getLoaderMutex() also has to outlive class_loader_ptrs_. With only the console_bridge fix, the process still aborted at exit, this time with recursive_mutex lock failed. The new death test fails on current rolling and passes with both fixes. Details are in the updated description.

🤖 Claude (AI assistant), on behalf of @Tobias-Fischer

Tobias-Fischer added a commit to RoboStack/robostack that referenced this pull request Nov 22, 2026
…static order) (#243)

* jazzy, lyrical, rolling: class_loader keeps console_bridge alive until exit

On exit, class_loader's static ClassLoaderPtrVectorImpl destroys the still-held
ClassLoaders, and ~ClassLoader() logs through console_bridge, whose output handler (a
function-local static) can already be gone: "mutex lock failed", std::terminate, exit 134
(e.g. `rqt --list-plugins` on macOS). ClassLoaderDependency() already orders class_loader's
own statics before class_loader_ptrs_; console_bridge::getLogLevel() there does the same for
console_bridge's. Same change as ros/class_loader#252. humble's class_loader 2.2.0 has no
ClassLoaderDependency and doesn't abort.

lyrical/rolling: the previous patch only made getAvailableClassesForLibrary's loader const,
which upstream already has in 2.9.4/3.0.2 (on rolling it applied with an offset to another
occurrence); it is replaced by this fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* class_loader: regression test for the exit order; rolling also keeps the loader mutex alive

- shared/tests/ros2-class-loader.yaml: builds a small plugin and a program that loads it
  through a MultiLibraryClassLoader it never destroys, and checks that the program exits 0.
  Without the fix it aborts on macOS ("mutex lock failed", exit 134) with the published
  lyrical/rolling class_loader; it passes with this PR's builds on jazzy, lyrical and rolling
  (and with humble's 2.2.0).
- rolling (class_loader 3.0.2): the test also caught that 3.0's getLoaderMutex() (a static
  added for concurrent load/unload) isn't in ClassLoaderDependency either, so
  ~ClassLoader()'s unload at exit locked a destroyed recursive_mutex. It is now declared in
  class_loader_core.hpp and constructed in ClassLoaderDependency() as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant