Use CAFFE2_USE_MSVC_STATIC_RUNTIME to determine when to avoid waiting for global destructors on Windows (#43532)

Summary:
We are trying to build libtorch statically (BUILD_SHARED_LIBS=OFF) then link it into a DLL. Our setup hits the infinite loop mentioned [here](54c05fa34e/torch/csrc/autograd/engine.cpp (L228)) because we build with `BUILD_SHARED_LIBS=OFF` but still link it all into a DLL at the end of the day.

This PR fixes the issue by changing the condition to guard on which windows runtime the build links against using the `CAFFE2_USE_MSVC_STATIC_RUNTIME` flag. `CAFFE2_USE_MSVC_STATIC_RUNTIME` defaults to ON when `BUILD_SHARED_LIBS=OFF`, so backwards compatibility is maintained.

I'm not entirely confident I understand the subtleties of the windows runtime versus linking setup, but this setup works for us and should not affect the existing builds.

Fixes https://github.com/pytorch/pytorch/issues/44470

Pull Request resolved: https://github.com/pytorch/pytorch/pull/43532

Reviewed By: mrshenli

Differential Revision: D24053767

Pulled By: albanD

fbshipit-source-id: 1127fefe5104d302a4fc083106d4e9f48e50add8
This commit is contained in:
Abaho Katabarwa 2020-10-01 16:39:01 -07:00 committed by Facebook GitHub Bot
parent 4f685ecc25
commit de3a48013a
5 changed files with 5 additions and 1 deletions

View file

@ -17,6 +17,7 @@ set(C10_USE_GFLAGS ${USE_GFLAGS}) # used in cmake_macros.h.in
set(C10_USE_GLOG ${USE_GLOG}) # used in cmake_macros.h.in
set(C10_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS}) # used in cmake_macros.h.in
set(C10_USE_NUMA ${USE_NUMA})
set(C10_USE_MSVC_STATIC_RUNTIME ${CAFFE2_USE_MSVC_STATIC_RUNTIME})
configure_file(
${CMAKE_CURRENT_LIST_DIR}/macros/cmake_macros.h.in
${CMAKE_BINARY_DIR}/c10/macros/cmake_macros.h)

View file

@ -8,6 +8,7 @@
#cmakedefine C10_USE_GLOG
#cmakedefine C10_USE_GFLAGS
#cmakedefine C10_USE_NUMA
#cmakedefine C10_USE_MSVC_STATIC_RUNTIME
// Used by libtorch mobile build to enable features that are not enabled by
// caffe2 mobile build. Should only use it when necessary as we are committed

View file

@ -44,6 +44,7 @@ function(caffe2_print_configuration_summary)
message(STATUS " Python site-packages: ${PYTHON_SITE_PACKAGES}")
endif()
message(STATUS " BUILD_SHARED_LIBS : ${BUILD_SHARED_LIBS}")
message(STATUS " CAFFE2_USE_MSVC_STATIC_RUNTIME : ${CAFFE2_USE_MSVC_STATIC_RUNTIME}")
message(STATUS " BUILD_TEST : ${BUILD_TEST}")
message(STATUS " BUILD_JNI : ${BUILD_JNI}")
message(STATUS " BUILD_MOBILE_AUTOGRAD : ${BUILD_MOBILE_AUTOGRAD}")

View file

@ -245,6 +245,7 @@ class CMake:
'MKL_THREADING',
'MKLDNN_CPU_RUNTIME',
'MSVC_Z7_OVERRIDE',
'CAFFE2_USE_MSVC_STATIC_RUNTIME',
'Numa_INCLUDE_DIR',
'Numa_LIBRARIES',
'ONNX_ML',

View file

@ -227,7 +227,7 @@ Engine::~Engine() {
// Do not wait for termination of global threads on Windows
// Because CRT terminates DLL threads before calling
// global object destructors
#if !defined(_WIN32) || !defined(C10_BUILD_SHARED_LIBS)
#if !defined(_WIN32) || defined(C10_USE_MSVC_STATIC_RUNTIME)
std::unique_lock<std::mutex> lk(non_reentrant_device_thread_mutex_);
while(non_reentrant_device_thread_count_.load() != 0) {
non_reentrant_device_thread_condvar_.wait(lk);