[DISCUSS][RFC] Static Destruction Order Problem
A crash occurs when exiting the process when at least one vulkan device has been constructed. This appears to be due to the way static destruction order interacts with library unloading order on Windows (a similar issue appeared previously with NNVM).
The core of the previous issue, and seemingly the same issue here, is that when using static destructors in libraries, it is easy to run into a situation where one library is calling into another after the other has been unloaded. I suspect in this case, the vulkan library is unloaded before TVM's static destructors are invoked, leading to a crash when trying to destroy vulkan devices.
The crash occurs here, on the call to vkDestroyDevice: https://github.com/dmlc/tvm/blob/master/src/runtime/vulkan/vulkan_device_api.cc#L16
We can check that this is due to the destruction/library unload order by forcing this destructor to be called before any libraries are unloaded, by calling it at the end of main(). This requires a small modification to the destructor so that it won't fail when called twice:
VulkanWorkspace::~VulkanWorkspace() {
for (VulkanContext& ctx : context_) {
vkDestroyDevice(ctx.device, nullptr);
}
if (instance_ != nullptr) {
vkDestroyInstance(instance_, nullptr);
instance_ = nullptr;
}
}
and then making the following call at the end of main()
TVMContext vulkan_ctx{ (DLDeviceType)kDLVulkan, 0 };
tvm::runtime::DeviceAPI::Get(vulkan_ctx)->~DeviceAPI();
stops the issue from occurring entirely.
However, this is quite a hacky solution, and doing this from python is even more cumbersome, especially if the process doesn't exit cleanly.
Is there a better way of handling this cleanup that can prevent these issues?
More generally, there may be a wider issue of relying on lifetimes of static variables that keeps manifesting in this kind of issue. Is there a better way of handling library-wide lifetimes that could prevent this as the project grows? The most direct solution I can think of is requiring library clients to call a cleanup function when they are done with the library, eg TVMDestroy (for C++ this could have a RAII wrapper that is constructed in main()). This is more onerous, but would guarantee that the cleanup code is always run before any libraries have been unloaded in the course of shutting down the process. It could also be used to allow other libraries like NNVM and TOPI to register their own cleanup functions with TVM, and TVM can ensure that their cleanup happens first.
I'd be interested to hear any thoughts on how this specific issue can be resolved easily, and more generally this type of destructor/library unload issue in the future.
Unfortunately, static destruction order problem does occur in certain cases. There are several ways to alleviate this problem
- For resources that we can control, always obtain a shared_ptr of a global singleton you dependent on(so destruction happens after that).
- I do agree that having an eager destructor could help in certain cases, as long as the destructor itself is idempotent (if it is called twice the second time have no effect). Let us say this function is called TVMFinalize. In cases when this function is not called, the destructor will still be called automatically.
Having thought about this some more, I think that an eager destructor is necessary to resolve this issue. The issue is not just destruction order across statics in the project (although it is important to have a robust way of dealing with this), the issue is that, on windows at least, some libraries may be unloaded before others have their static destructors invoked, and AFAIK we cannot control this ordering.
Fundamentally, when any static destructor runs from a library, we cannot know what other libraries have already been unloaded by the OS (and that includes libraries like the vuklan API lib), so making calls across library boundaries in static destructors is potentially unsafe.
The only way I can think of resolving this is with an eager destructor because we know no libraries have been unloaded when it is called.
Definitely agree it should be idempotent.
I'm not sure it should be called automatically if the app doesn't call it - this is likely to trigger exactly this kind of hard to debug problem, and if the process is being torn down anyway the OS will handle the cleanup.
if the eager destructor is not called automatically, the default destructor of these singleton object will get called, and will hopefully exit correctly given. I do agree that having an exit hook that calls into finalize is nice(although in certain cases this creates an additional burden on the user).
For drivers like CUDA, we can simply catch the error code saying the driver has been unloaded and ignore them during destruction time. But that really depends on how robust the driver is
Ah - I wasn't specific before but the error raised when calling vkDestroyDevice is an access violation, not a catchable exception. This was because the vuklan API library had been unloaded by the OS before TVM's static destructors were called.
This means that any cross library function calls in static destructors can potentially cause an un-catchable error, and I'm not sure there is a safe way to determine if a library you depend on has been unloaded when the static destructor runs.
As far as I can see the only way to prevent this is to require an eager destructor for any destructor that may make cross-library calls (which could be as subtle as releasing a reference to a registered shared_ptr) in static destructors.
I think it makes sense to have eager idempotent destructor. But we do also want to be compatible with cases when TVMFinalize is not called, as it works for some cases depending on how good the driver is.
So a good middle ground could be support TVMFinalize and encourage a user to use it in runtime, while still support the old use case
I don't think that will work as a solution; since we cannot be sure (AFAIK) when a static destructor is running whether any other DLL has been unloaded from the process' address space then making cross library calls is undefined behaviour.
It so happens that this works on linux, but this is implementation dependant.
This goes for any cross library calls - in this case it is tvm->vulkan but before it was tvm->nnvm via callback.
If we implement the destructor being automatically called, then this will cause an error on windows whenever the process exits and TVMFinalize has not been called. This could easily occur when, for example a python program exits with an error. I think the correct approach would be to just let the OS clean up if the program does not. See this post: https://blogs.msdn.microsoft.com/oldnewthing/20120105-00/?p=8683/
I will have another look later to see if there is a way that we can enforce a certain order of unloading DLLs at process exit, but as far as I know this is not possible. Even if it is possible, it is likely to be Windows specific, so it won't guarantee that other non-linux and non-windows platforms will function correctly.
Summary of the current status of static destructors:
- The external FUNCTOR registration problem has been resolved as we move to register function pointer instead of std::function
- We have decided to not destruct most of the resources(function pointers) in the singletons, to avoid destruction order problem
- We have not resolved the order dependencies of DeviceAPI, the current recommendation is always de-allocate array in functions instead of keeping a global reference. For these with global reference, never destruct them.
Given there are not too many further actionable items, we will close this thread for now.