diff --git a/inference-engine/src/plugin_api/cpp_interfaces/impl/ie_infer_async_request_thread_safe_default.hpp b/inference-engine/src/plugin_api/cpp_interfaces/impl/ie_infer_async_request_thread_safe_default.hpp index b88a03531a445e..c82a9ebb303c4f 100644 --- a/inference-engine/src/plugin_api/cpp_interfaces/impl/ie_infer_async_request_thread_safe_default.hpp +++ b/inference-engine/src/plugin_api/cpp_interfaces/impl/ie_infer_async_request_thread_safe_default.hpp @@ -404,15 +404,18 @@ class AsyncInferRequestThreadSafeDefault : public IInferRequestInternal { { std::lock_guard lock{_mutex}; _state = InferState::Idle; - callback = _callback; + std::swap(callback, _callback); } if (callback) { try { - auto local_callback = std::move(callback); - local_callback(currentException); + callback(currentException); } catch (...) { currentException = std::current_exception(); } + std::lock_guard lock{_mutex}; + if (!_callback) { + std::swap(callback, _callback); + } } if (nullptr == currentException) { promise.set_value(); diff --git a/inference-engine/tests/functional/plugin/shared/include/behavior/infer_request/callback.hpp b/inference-engine/tests/functional/plugin/shared/include/behavior/infer_request/callback.hpp index 7ed969f5e82f13..ee18be2728f11f 100644 --- a/inference-engine/tests/functional/plugin/shared/include/behavior/infer_request/callback.hpp +++ b/inference-engine/tests/functional/plugin/shared/include/behavior/infer_request/callback.hpp @@ -145,4 +145,21 @@ TEST_P(InferRequestCallbackTests, ReturnResultNotReadyFromWaitInAsyncModeForTooS ASSERT_NO_THROW(req.Wait(InferenceEngine::InferRequest::WaitMode::RESULT_READY)); } +TEST_P(InferRequestCallbackTests, ImplDoseNotCopyCallback) { + // Skip test according to plugin specific disabledTestPatterns() (if any) + SKIP_IF_CURRENT_TEST_IS_DISABLED() + InferenceEngine::CNNNetwork cnnNet(function); + auto execNet = ie->LoadNetwork(cnnNet, targetDevice, configuration); + auto req = execNet.CreateInferRequest(); + { + auto somePtr = std::make_shared(42); + req.SetCompletionCallback([somePtr] { + ASSERT_EQ(1, somePtr.use_count()); + }); + } + + ASSERT_NO_THROW(req.StartAsync()); + ASSERT_NO_THROW(req.Wait(InferenceEngine::InferRequest::WaitMode::RESULT_READY)); +} + } // namespace BehaviorTestsDefinitions \ No newline at end of file