@@ -130,7 +130,7 @@ PyParametersToJSON(const py::dict& parameters)
130130void
131131AsyncEventFutureDoneCallback (const py::object& py_future)
132132{
133- std::unique_ptr<Stub>& stub = Stub::GetOrCreateInstance ();
133+ auto stub = Stub::GetOrCreateInstance ();
134134 stub->BackgroundFutureDone (py_future);
135135}
136136
@@ -514,7 +514,7 @@ Stub::AutoCompleteModelConfig(
514514 python_backend_utils.def (
515515 " get_model_dir" ,
516516 []() {
517- std::unique_ptr<Stub>& stub = Stub::GetOrCreateInstance ();
517+ auto stub = Stub::GetOrCreateInstance ();
518518 return stub->GetModelDir ();
519519 },
520520 py::return_value_policy::reference);
@@ -568,7 +568,7 @@ Stub::Initialize(bi::managed_external_buffer::handle_t map_handle)
568568 python_backend_utils.def (
569569 " get_model_dir" ,
570570 []() {
571- std::unique_ptr<Stub>& stub = Stub::GetOrCreateInstance ();
571+ auto stub = Stub::GetOrCreateInstance ();
572572 return stub->GetModelDir ();
573573 },
574574 py::return_value_policy::reference);
@@ -1073,16 +1073,22 @@ Stub::~Stub()
10731073 memory_manager_message_queue_.reset ();
10741074}
10751075
1076- std::unique_ptr< Stub> Stub::stub_instance_ ;
1076+ static std::shared_ptr<triton::backend::python:: Stub> stub_instance{ nullptr } ;
10771077
1078- std::unique_ptr< Stub>&
1078+ std::shared_ptr<triton::backend::python:: Stub>
10791079Stub::GetOrCreateInstance ()
10801080{
1081- if (Stub::stub_instance_. get () == nullptr ) {
1082- Stub::stub_instance_ = std::make_unique< Stub>( );
1081+ if (!stub_instance ) {
1082+ stub_instance. reset ( new triton::backend::python:: Stub() );
10831083 }
10841084
1085- return Stub::stub_instance_;
1085+ return stub_instance;
1086+ }
1087+
1088+ void
1089+ Stub::DestroyInstance ()
1090+ {
1091+ stub_instance.reset ();
10861092}
10871093
10881094void
@@ -1822,7 +1828,7 @@ PYBIND11_EMBEDDED_MODULE(c_python_backend_utils, module)
18221828 " exec" ,
18231829 [](std::shared_ptr<InferRequest>& infer_request,
18241830 const bool decoupled) {
1825- std::unique_ptr<Stub>& stub = Stub::GetOrCreateInstance ();
1831+ auto stub = Stub::GetOrCreateInstance ();
18261832 std::shared_ptr<InferResponse> response =
18271833 infer_request->Exec (decoupled);
18281834 py::object response_object;
@@ -1840,7 +1846,7 @@ PYBIND11_EMBEDDED_MODULE(c_python_backend_utils, module)
18401846 " async_exec" ,
18411847 [](std::shared_ptr<InferRequest>& infer_request,
18421848 const bool decoupled) {
1843- std::unique_ptr<Stub>& stub = Stub::GetOrCreateInstance ();
1849+ auto stub = Stub::GetOrCreateInstance ();
18441850 py::object loop =
18451851 py::module_::import (" asyncio" ).attr (" get_running_loop" )();
18461852 py::cpp_function callback = [&stub, infer_request, decoupled]() {
@@ -2125,7 +2131,7 @@ main(int argc, char** argv)
21252131 std::string name = argv[8 ];
21262132 std::string runtime_modeldir = argv[9 ];
21272133
2128- std::unique_ptr<Stub>& stub = Stub::GetOrCreateInstance ();
2134+ auto stub = Stub::GetOrCreateInstance ();
21292135 try {
21302136 stub->Instantiate (
21312137 shm_growth_size, shm_default_size, shm_region_name, model_path,
@@ -2135,7 +2141,7 @@ main(int argc, char** argv)
21352141 catch (const PythonBackendException& pb_exception) {
21362142 LOG_INFO << " Failed to preinitialize Python stub: " << pb_exception.what ();
21372143 logger.reset ();
2138- stub. reset ();
2144+ Stub::DestroyInstance ();
21392145 exit (1 );
21402146 }
21412147
@@ -2148,7 +2154,7 @@ main(int argc, char** argv)
21482154#endif
21492155 std::atomic<bool > background_thread_running = {true };
21502156 std::thread background_thread =
2151- std::thread ([&parent_pid , &background_thread_running , &stub , &logger] {
2157+ std::thread ([stub , &parent_pid , &background_thread_running , &logger] {
21522158 // Send a dummy message after the stub process is launched to notify the
21532159 // parent process that the health thread has started.
21542160 std::unique_ptr<IPCMessage> ipc_message = IPCMessage::Create (
@@ -2180,7 +2186,7 @@ main(int argc, char** argv)
21802186
21812187 // Destroy stub and exit.
21822188 logger.reset ();
2183- stub. reset ();
2189+ Stub::DestroyInstance ();
21842190 exit (1 );
21852191 }
21862192 }
@@ -2213,7 +2219,7 @@ main(int argc, char** argv)
22132219 // this process will no longer hold the GIL lock and destruction of the stub
22142220 // will result in segfault.
22152221 logger.reset ();
2216- stub. reset ();
2222+ Stub::DestroyInstance ();
22172223
22182224 return 0 ;
22192225}
0 commit comments