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.
- 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::spawnmay also access some thread locals.
Does anyone have any suggestions about how to avoid panics due to thread locals being destroyed?