Thread locals destroyed when destructing C++ static objects

We are building a Rust library used in C++, and we use tokio crate for the async runtime which uses thread locals heavily. However, it panics when destructing C++ static objects, because those C++ objects' destructors call some Rust functions that may access thread locals to clear some global resources, and the thread locals are all destroyed before clearing the destructors of static objects.

Given that:

  • We may not touch the C++ user codes, and some of the user codes are even inaccessible to us;
  • We must do resource clearing before the process exits because there are inter-process maps to release;
  • We have tried to spawn a temporary thread to make sure the thread locals are accessible, but there seems to be too many patches to apply, and even std::thread::spawn may also access some thread locals.

Does anyone have any suggestions about how to avoid panics due to thread locals being destroyed?

Maybe to test if the thread locals are destroyed in the C++-call-Rust entry point, and if it is destroyed, spawn a new thread to call the Rust API.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.