Skip to content

Commit ff30c9f

Browse files
committed
fix for hang on windows
1 parent efc629e commit ff30c9f

File tree

6 files changed

+34
-12
lines changed

6 files changed

+34
-12
lines changed

src/inference/dev_api/openvino/runtime/iplugin.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ class OPENVINO_RUNTIME_API IPlugin : public std::enable_shared_from_this<IPlugin
236236

237237
virtual ~IPlugin() = default;
238238

239+
/**
240+
* @brief Explicit resource cleanup before plugin unload
241+
*/
242+
virtual void cleanup() {};
243+
239244
protected:
240245
IPlugin();
241246

src/inference/src/cpp/core.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ void Core::unload_plugin(const std::string& device_name) {
271271
OV_CORE_CALL_STATEMENT({
272272
ov::DeviceIDParser parser(device_name);
273273
std::string devName = parser.get_device_name();
274-
274+
_impl->get_plugin(devName).cleanup();
275275
_impl->unload_plugin(devName);
276276
});
277277
}

src/inference/src/dev/plugin.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
OPENVINO_THROW("Unexpected exception"); \
2121
}
2222

23-
ov::Plugin::~Plugin() {
24-
m_ptr = {};
25-
}
26-
2723
ov::Plugin::Plugin(const std::shared_ptr<ov::IPlugin>& ptr, const std::shared_ptr<void>& so) : m_ptr{ptr}, m_so{so} {
2824
OV_PLUGIN_CALL_STATEMENT();
2925
}

src/inference/src/dev/plugin.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class Plugin {
2828
public:
2929
Plugin() = default;
3030

31-
~Plugin();
32-
3331
Plugin(const std::shared_ptr<ov::IPlugin>& ptr, const std::shared_ptr<void>& so);
3432

3533
void set_name(const std::string& deviceName);
@@ -81,6 +79,8 @@ class Plugin {
8179
return get_property(property.name(), arguments).template as<T>();
8280
}
8381
bool supports_model_caching(const AnyMap& arguments = {}) const;
82+
83+
void cleanup() { m_ptr->cleanup(); }
8484
};
8585

8686
} // namespace ov

src/plugins/intel_gpu/include/intel_gpu/plugin/plugin.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Plugin : public ov::IPlugin {
2020
std::map<std::string, ExecutionConfig> m_configs_map;
2121
ov::AnyMap m_compiled_model_runtime_properties;
2222

23+
mutable std::map<std::string, std::shared_ptr<RemoteContextImpl>> m_default_contexts;
24+
mutable std::once_flag m_default_contexts_once;
2325
std::map<std::string, std::shared_ptr<RemoteContextImpl>> get_default_contexts() const;
2426

2527
std::shared_ptr<ov::Model> clone_and_transform_model(const std::shared_ptr<const ov::Model>& network,
@@ -67,6 +69,7 @@ class Plugin : public ov::IPlugin {
6769
const ov::AnyMap& properties) const override;
6870
ov::SoPtr<ov::IRemoteContext> create_context(const ov::AnyMap& remote_properties) const override;
6971
ov::SoPtr<ov::IRemoteContext> get_default_context(const ov::AnyMap& remote_properties) const override;
72+
void cleanup() override;
7073
};
7174

7275
} // namespace ov::intel_gpu

src/plugins/intel_gpu/src/plugin/plugin.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,31 @@ std::shared_ptr<ov::Model> Plugin::clone_and_transform_model(const std::shared_p
200200
return cloned_model;
201201
}
202202

203-
std::map<std::string, RemoteContextImpl::Ptr> Plugin::get_default_contexts() const {
204-
static std::map<std::string, std::shared_ptr<RemoteContextImpl>> m_default_contexts;
205-
static std::once_flag m_default_contexts_once;
203+
// weak map to hold singleton default contexts
204+
// Weak singleton map is used to share contexts between multiple plugin(or Core) instances.
205+
// It is needed to ensure that context is released before plugin is unloaded.
206+
// As the actual ownership is in plugin class, the ownership is released when plugin is destructed.
207+
std::map<std::string, std::weak_ptr<RemoteContextImpl>> weak_singleton_default_contexts;
208+
std::mutex singleton_default_contexts_mutex;
206209

210+
std::map<std::string, RemoteContextImpl::Ptr> Plugin::get_default_contexts() const {
207211
std::call_once(m_default_contexts_once, [this]() {
208-
// Create default context
212+
std::lock_guard<std::mutex> lock(singleton_default_contexts_mutex);
209213
for (auto& device : m_device_map) {
210214
const auto device_name = get_device_name() + "." + device.first;
215+
216+
// If already initialized, use existing one
217+
if (weak_singleton_default_contexts.find(device.first) != weak_singleton_default_contexts.end()) {
218+
if (auto ctx = weak_singleton_default_contexts[device.first].lock()) {
219+
m_default_contexts[device.first] = ctx;
220+
continue;
221+
}
222+
}
223+
// If context is not created yet or expired, create new one
211224
const auto initialize = false;
212225
auto ctx = std::make_shared<RemoteContextImpl>(device_name, std::vector<cldnn::device::ptr>{device.second}, initialize);
213-
m_default_contexts.insert({device.first, ctx});
226+
weak_singleton_default_contexts[device.first] = ctx;
227+
m_default_contexts[device.first] = ctx;
214228
}
215229
});
216230
return m_default_contexts;
@@ -972,6 +986,10 @@ uint32_t Plugin::get_optimal_batch_size(const ov::AnyMap& options) const {
972986

973987
return batch;
974988
}
989+
void Plugin::cleanup() {
990+
GPU_DEBUG_INFO << "[GPU] Plugin shutdown" << std::endl;
991+
m_default_contexts.clear();
992+
}
975993

976994
} // namespace ov::intel_gpu
977995

0 commit comments

Comments
 (0)