Skip to content

Unbind destroyed ClassLoaders from their library, fix ownership data races, add stress tests - #247

Merged
mjcarroll merged 3 commits into
rollingfrom
mjcarroll/stress-and-lifetime-tests
Dec 3, 2026
Merged

mjcarroll merged 3 commits into
rollingfrom
mjcarroll/stress-and-lifetime-tests

Conversation

@mjcarroll

@mjcarroll mjcarroll commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Description

Two fixes, in separate commits:

  • A ClassLoader destroyed while its instances were alive, or after more than one loadLibrary(), stayed registered as an owner of the library's classes and never unloaded the library. A later ClassLoader at the same address then appeared to own those classes. The destructor now always unbinds; surviving managed instances keep the library open until the last one goes away.
  • isLibraryLoaded() and the allMetaObjectsFor*() helpers read metaobject owners without the factory map lock, and createInstance() checked ownership after releasing it. With concurrent MultiLibraryClassLoaders, TSan reported about 150 races and createInstance() sometimes threw "no factory exists" for a loaded class. These now hold the lock, keeping the existing lock order.

Is this user-facing behavior change?

No.

Did you use Generative AI?

Claude Code (Claude Opus 5.5) wrote the change; I reviewed it.

Additional Information

New tests: ClassLoaderLifetimeTest.*, where two fail on rolling and one is skipped on Windows (uses dlopen(RTLD_NOLOAD)), and ClassLoaderStressTest.*, which is most useful with #246's TSan instrumentation. Touches code near #243 and #244; whichever merges second needs a small rebase.

@mjcarroll mjcarroll self-assigned this Nov 10, 2026
@mjcarroll
mjcarroll marked this pull request as draft November 10, 2026 03:26
@mjcarroll
mjcarroll force-pushed the mjcarroll/stress-and-lifetime-tests branch from 4b09b63 to 6516ab0 Compare November 28, 2026 03:09
A ClassLoader destroyed while instances it created were still alive (in
either mode), or after loadLibrary() was called more than once, never
unloaded its library and stayed registered as an owner of the library's
classes. The library then stayed loaded for the rest of the process,
and a later ClassLoader allocated at the same address, even one for a
different library, appeared to own those classes.

Now the destructor always unbinds the loader from its library. If
managed instances are still alive, it first opens another handle to
the library for them, and the last of them closes it.

Signed-off-by: Michael Carroll <[email protected]>
isLibraryLoaded() and the allMetaObjectsFor*() helpers filtered
metaobjects without holding the factory map lock, while other threads
changed their owners or destroyed them under it, and createInstance()
checked ownership after releasing it. ThreadSanitizer reported these
races, and loading and unloading the same library on several threads
could fail to find a class that was loaded.

Check and filter under the lock, keeping the existing lock order.

Add stress tests for loaders and MultiLibraryClassLoaders sharing
libraries across threads, and for instances released on other threads
while their loader is destroyed.

Signed-off-by: Michael Carroll <[email protected]>
@mjcarroll
mjcarroll force-pushed the mjcarroll/stress-and-lifetime-tests branch from 6516ab0 to 8c80b09 Compare November 28, 2026 13:29
@mjcarroll
mjcarroll marked this pull request as ready for review November 28, 2026 13:30
@mjcarroll
mjcarroll requested a review from ahcorde November 28, 2026 13:30
When a ClassLoader is destroyed while its instances are still alive,
open the library again by its resolved path. If that fails, pin the
library and still unbind the loader, so no dangling owner is left.
Close the extra handle under the loader mutex, and catch everything
in the destructor. Also hold the lock in
getAllLibrariesUsedByClassLoader(), and group the lifetime tests.

Signed-off-by: Alejandro Hernandez Cordero <[email protected]>
@ahcorde

ahcorde commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Pulls: #247
Gist: https://gist.githubusercontent.com/ahcorde/75ea27db470178aa3f934f2a83a0e31f/raw/179932e1840994e558595147984431d4e786591e/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/21406

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

@mjcarroll

Copy link
Copy Markdown
Member Author

Pulls: #247
Gist: https://gist.githubusercontent.com/mjcarroll/69dad235a6301643617f3ded8f87c036/raw/179932e1840994e558595147984431d4e786591e/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/21451

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

@mjcarroll
mjcarroll merged commit b169e65 into rolling Dec 3, 2026
14 of 16 checks passed
@ahcorde
ahcorde deleted the mjcarroll/stress-and-lifetime-tests branch December 3, 2026 15:12
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