Skip to content

[CORE] Make sure callback finishes execution before running it the second time for the next run in separate thread#36304

Open
pereanub wants to merge 5 commits into
openvinotoolkit:masterfrom
pereanub:make_sure_callback_finishes_exec
Open

[CORE] Make sure callback finishes execution before running it the second time for the next run in separate thread#36304
pereanub wants to merge 5 commits into
openvinotoolkit:masterfrom
pereanub:make_sure_callback_finishes_exec

Conversation

@pereanub

@pereanub pereanub commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Details:

This change fixes an async callback race where callback delivery could be missed for a request when the callback restarts inference and holds execution. In stressed scenarios, this could leave accounting's checkout inconsistent and cause pending-request hangs.

Expected outcome:
No unexpected “empty/missed callback” symptom under callback re-entry stress.
No pending-request stall caused by callback/accounting mismatch.
Deterministic failure if callback delivery regresses again. (NPU test case)

Tickets:

AI Assistance:

  • AI assistance used: yes
  • debug ideas

@pereanub pereanub requested review from a team as code owners June 8, 2026 11:40
@github-actions github-actions Bot added category: inference OpenVINO Runtime library - Inference category: NPU OpenVINO NPU plugin labels Jun 8, 2026
@pereanub pereanub changed the title [CORE[ Make sure callback finishes execution before running it the second time for the next run [CORE] Make sure callback finishes execution before running it the second time for the next run Jun 8, 2026
@pereanub pereanub force-pushed the make_sure_callback_finishes_exec branch from e6bebcd to be3872c Compare June 8, 2026 11:44
@pereanub pereanub requested a review from Copilot June 8, 2026 11:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses an async callback re-entrancy race in ov::IAsyncInferRequest where a callback that restarts inference and blocks execution could cause subsequent callback delivery to be missed, leading to accounting/pending-request hangs. It also adds an Intel NPU functional test intended to deterministically catch callback-miss regressions under callback re-entry + delay stress.

Changes:

  • Serialize async callback execution by introducing a new mutex (m_callback_invoke_mutex) and using it during callback assignment/invocation/clearing in IAsyncInferRequest.
  • Adjust callback handling in the final pipeline stage to avoid the previous “swap-out callback” behavior that could cause missed callbacks under re-entrancy.
  • Add an Intel NPU functional test that restarts async inference from within the callback and sleeps to stress callback delivery ordering.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/infer_request_run.hpp Adds a regression test for callback re-entry with sleep to detect missed/empty callback delivery.
src/inference/src/dev/iasync_infer_request.cpp Changes callback locking/invocation behavior to prevent overlapping callback executions.
src/inference/dev_api/openvino/runtime/iasync_infer_request.hpp Adds m_callback_invoke_mutex to support new callback synchronization.

Comment thread src/inference/src/dev/iasync_infer_request.cpp
@pereanub pereanub force-pushed the make_sure_callback_finishes_exec branch 2 times, most recently from 9336030 to 89ebe0b Compare June 8, 2026 12:12
@pereanub pereanub requested a review from Copilot June 8, 2026 12:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread src/inference/src/dev/iasync_infer_request.cpp
@pereanub pereanub marked this pull request as draft June 8, 2026 12:56
@pereanub pereanub force-pushed the make_sure_callback_finishes_exec branch from 89ebe0b to 1d2c44d Compare June 9, 2026 09:16
@pereanub pereanub requested a review from Copilot June 9, 2026 09:16
@github-actions github-actions Bot added the category: IE Tests OpenVINO Test: plugins and common label Jun 9, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Comment thread src/inference/src/dev/iasync_infer_request.cpp Outdated
Comment thread src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/callback.hpp Outdated
@pereanub pereanub force-pushed the make_sure_callback_finishes_exec branch from 1d2c44d to 6e9c728 Compare June 9, 2026 09:39
@pereanub pereanub requested a review from Copilot June 9, 2026 09:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Comment thread src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/callback.hpp Outdated
Comment thread src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/callback.hpp Outdated
@pereanub pereanub force-pushed the make_sure_callback_finishes_exec branch from 6e9c728 to 5881d3d Compare June 9, 2026 09:54
@pereanub pereanub requested a review from Copilot June 9, 2026 09:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Comment on lines +292 to +295
mutable std::mutex m_mutex;
mutable std::mutex m_callback_invoke_mutex;
std::function<void(std::exception_ptr)> m_callback;
std::exception_ptr m_pending_callback_exception = nullptr;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's first agree on the changes, and after that, update the doc if needed.

Comment thread src/inference/dev_api/openvino/runtime/iasync_infer_request.hpp Outdated
Comment thread src/inference/dev_api/openvino/runtime/iasync_infer_request.hpp Outdated
@pereanub pereanub force-pushed the make_sure_callback_finishes_exec branch from 5881d3d to e100d1d Compare June 9, 2026 10:56
@pereanub pereanub requested a review from Copilot June 9, 2026 10:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Comment on lines +292 to +297
mutable std::mutex m_mutex;
mutable std::recursive_mutex m_callback_invoke_mutex;
std::function<void(std::exception_ptr)> m_callback;
// Exception thrown by user callback is captured and rethrown on the next start_async()/infer() call.
// This avoids deadlocks when callbacks re-enter inference on the same request.
std::exception_ptr m_pending_callback_exception = nullptr;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's first agree on the changes, and after that, update the doc if needed.

Comment on lines 292 to 294
mutable std::mutex m_mutex;
mutable std::recursive_mutex m_callback_invoke_mutex;
std::function<void(std::exception_ptr)> m_callback;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but I think recursive_mutex is better to keep it more appropriate to the older approach as well.

@pereanub pereanub marked this pull request as ready for review June 9, 2026 11:17
@pereanub pereanub requested review from a team as code owners June 9, 2026 11:17
@pereanub pereanub changed the title [CORE] Make sure callback finishes execution before running it the second time for the next run [CORE] Make sure callback finishes execution before running it the second time for the next run in separate thread Jun 9, 2026
pereanub added 4 commits June 9, 2026 12:20
…infer

Signed-off-by: Bogdan Pereanu <bogdan.pereanu@intel.com>
Signed-off-by: Bogdan Pereanu <bogdan.pereanu@intel.com>
Signed-off-by: Bogdan Pereanu <bogdan.pereanu@intel.com>
Signed-off-by: Bogdan Pereanu <bogdan.pereanu@intel.com>
@pereanub pereanub force-pushed the make_sure_callback_finishes_exec branch from 19ed7bf to 2aac8d1 Compare June 9, 2026 11:21
@pereanub pereanub marked this pull request as draft June 9, 2026 13:32
Signed-off-by: Bogdan Pereanu <bogdan.pereanu@intel.com>
@pereanub pereanub force-pushed the make_sure_callback_finishes_exec branch from 125dbd4 to 3fa22d2 Compare June 9, 2026 15:43
@pereanub pereanub marked this pull request as ready for review June 9, 2026 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: IE Tests OpenVINO Test: plugins and common category: inference OpenVINO Runtime library - Inference category: NPU OpenVINO NPU plugin

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants