@@ -1040,11 +1040,13 @@ Stub::~Stub()
10401040{
10411041#ifdef TRITON_ENABLE_GPU
10421042 try {
1043- CUDAHandler& cuda_api = CUDAHandler::getInstance ();
1044- for (auto & m :
1045- shm_pool_->GetCUDAMemoryPoolManager ()->CUDAPoolAddressMap ()) {
1046- if (m.second != nullptr ) {
1047- cuda_api.CloseCudaHandle (m.first , m.second );
1043+ if (shm_pool_ != nullptr ) {
1044+ CUDAHandler& cuda_api = CUDAHandler::getInstance ();
1045+ for (auto & m :
1046+ shm_pool_->GetCUDAMemoryPoolManager ()->CUDAPoolAddressMap ()) {
1047+ if (m.second != nullptr ) {
1048+ cuda_api.CloseCudaHandle (m.first , m.second );
1049+ }
10481050 }
10491051 }
10501052 }
@@ -1053,13 +1055,14 @@ Stub::~Stub()
10531055 }
10541056#endif
10551057
1056- {
1058+ // Ensure the interpreter is active before trying to clean up.
1059+ if (Py_IsInitialized ()) {
10571060 py::gil_scoped_acquire acquire;
10581061 py::object async_event_loop_local (std::move (async_event_loop_));
10591062 py::object background_futures_local (std::move (background_futures_));
10601063 py::object model_instance_local (std::move (model_instance_));
10611064 }
1062- stub_instance_. reset ();
1065+
10631066 stub_message_queue_.reset ();
10641067 parent_message_queue_.reset ();
10651068 stub_to_parent_mq_.reset ();
@@ -2030,6 +2033,7 @@ main(int argc, char** argv)
20302033 catch (const PythonBackendException& pb_exception) {
20312034 LOG_INFO << " Failed to preinitialize Python stub: " << pb_exception.what ();
20322035 logger.reset ();
2036+ stub.reset ();
20332037 exit (1 );
20342038 }
20352039
0 commit comments