Skip to content

Construct the loader mutex before the static loader vector - #253

Merged
mjcarroll merged 2 commits into
rollingfrom
fix-loader-mutex-destruction-order
Nov 24, 2026
Merged

mjcarroll merged 2 commits into
rollingfrom
fix-loader-mutex-destruction-order

Conversation

@mjcarroll

@mjcarroll mjcarroll commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Description

impl::unloadLibrary() locks getLoaderMutex() first, but ClassLoaderDependency does not force it to be constructed before the static ClassLoaderPtrVectorImpl. If a MultiLibraryClassLoader is the first user of class_loader, the mutex is destroyed before the vector, whose destructor then destroys a ClassLoader that locks it, and the process aborts at exit (recursive_mutex lock failed: Invalid argument).

Construct it from getLoadedLibraryVectorMutex(), which the dependency already forces. Adds a regression test (separate executable, loader kept alive until static destruction); it fails without the fix.

Is this user-facing behavior change?

No.

Did you use Generative AI?

Yes, Claude Code (Claude Sonnet 5.5) was used to find the cause, write the fix and test.

Additional Information

Found while running the ROS 2 test suites under Bazel with libc++, where image_transport and point_cloud_transport tests crashed at exit.

impl::unloadLibrary() locks getLoaderMutex() first. ClassLoaderDependency
only forces the other function-local statics to be constructed before
the static ClassLoaderPtrVectorImpl, so when a MultiLibraryClassLoader is
the first user of class_loader, the loader mutex is first used after that
static was constructed and is destroyed before it. The vector's destructor
then destroys a ClassLoader, which locks the destroyed mutex and aborts at
exit with "recursive_mutex lock failed: Invalid argument".

Construct it from getLoadedLibraryVectorMutex(), which the dependency
already forces, and add a regression test that keeps a loader alive until
static destruction.

Signed-off-by: Michael Carroll <mjcarroll.oss@gmail.com>
@mjcarroll
mjcarroll force-pushed the fix-loader-mutex-destruction-order branch from 14e37b5 to 30f81dc Compare November 22, 2026 16:05
@mjcarroll mjcarroll self-assigned this Nov 22, 2026
Comment thread src/class_loader_core.cpp
Comment on lines +53 to +56
namespace
{
std::recursive_mutex & getLoaderMutex();
} // namespace

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than forward-declaring it like this, can we just move the getLoaderMutex block down below up here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@mjcarroll

Copy link
Copy Markdown
Member Author

Pulls: #253
Gist: https://gist.githubusercontent.com/mjcarroll/b01be26bd409227896e5454e38963616/raw/3904fb7d22c6f73a6cb2880ee5d90fa121cc054f/ros2.repos
BUILD args: --packages-above-and-dependencies class_loader
TEST args: --packages-above class_loader
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/21226

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

Signed-off-by: Michael Carroll <mjcarroll.oss@gmail.com>

@clalancette clalancette left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good with green CI.

@mjcarroll

Copy link
Copy Markdown
Member Author

Pulls: #253
Gist: https://gist.githubusercontent.com/mjcarroll/621ee2fdc09f563578bdabcabb711252/raw/3904fb7d22c6f73a6cb2880ee5d90fa121cc054f/ros2.repos
BUILD args: --packages-above-and-dependencies class_loader
TEST args: --packages-above class_loader
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/21245

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

@mjcarroll
mjcarroll merged commit 9157bdc into rolling Nov 24, 2026
5 checks passed
mjcarroll added a commit to mjcarroll/rcr-nightly that referenced this pull request Nov 24, 2026
…clcpp#3430); give class_loader's new multi_library_exit_test its own cc_test
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.

2 participants