This is a copy of #180086, with some new testing.
We're sometimes getting segfaults in torch::profiler::impl::onFunctionEnter() because we get back a raw pointer from GlobalStateManager::get(). The underlying state being tracked, however, is actually a std::shared_ptr. The thread that owns the std::shared_ptr sometimes destroys the object while other threads have a dangling raw pointer to the state.
The approach we take to fixing the problem is to always hand out an actual std::shared_ptr when the callback will be executed on another thread. That ensures that even if the thread that owns the state is joined, the state itself will still live as long as the thread has a valid copy of the std::shared_ptr.
Pull Request resolved: #187141
Approved by: https://github.com/ryanzhang22
Co-authored-by: Jiannan Wang [email protected]
SOCIAL SHARE CARD GENERATOR