dlclose Failure: A Weird Interplay Between Rust and C++ Libraries
This post recounts a perplexing debugging story: when using `dlclose` to unload a dynamic library, libA was successfully unloaded, but its dependency, libB, unexpectedly remained in memory. Investigation revealed the root cause was thread local storage (TLS) destructors registered in libB. Because the threads didn't exit, these destructors weren't executed, preventing libB from unloading. Enabling logging resolved the issue because the logging library also used TLS, preventing libA from unloading and thus maintaining consistent shared state between libA and libB. This case highlights the importance of understanding `dlclose` behavior and the impact of TLS destructors, recommending the use of the `LD_DEBUG` environment variable for debugging dynamic link libraries.
Read more