Repository navigation
Unbind destroyed ClassLoaders from their library, fix ownership data races, add stress tests - #247
Conversation
4b09b63 to
6516ab0
Compare
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]>
6516ab0 to
8c80b09
Compare
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]>
|
Pulls: #247 |
|
Pulls: #247 |
Description
Two fixes, in separate commits:
ClassLoaderdestroyed while its instances were alive, or after more than oneloadLibrary(), stayed registered as an owner of the library's classes and never unloaded the library. A laterClassLoaderat 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 theallMetaObjectsFor*()helpers read metaobject owners without the factory map lock, andcreateInstance()checked ownership after releasing it. With concurrentMultiLibraryClassLoaders, TSan reported about 150 races andcreateInstance()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 (usesdlopen(RTLD_NOLOAD)), andClassLoaderStressTest.*, which is most useful with #246's TSan instrumentation. Touches code near #243 and #244; whichever merges second needs a small rebase.