Fix shared_mutex reader starvation in MediapipeFactory::create() - #4471
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Updates MediapipeFactory::create() to prevent writer starvation by releasing the shared mutex before potentially blocking graph creation.
Changes:
- Unlocks
definitionsMtxbefore callingdefinition.create(). - Allows definition reload writers to acquire the mutex promptly.
Suppressed comments (1)
src/mediapipe_internal/mediapipefactory.cpp:145
- This synchronization fix needs a regression test for the actual starvation interleaving: exhaust a graph queue so
create()blocks ingetIdleStream(), then invokecreateDefinitionorreloadDefinitionon another thread and assert the writer completes before a queue slot is returned. The existing create/reload tests exercise these operations sequentially, so they would not catch a future reintroduction of the lock being held across the blocking call.
// Unlock before create() which may block on graph queue, avoiding writer starvation.
lock.unlock();
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
mzegla
approved these changes
Aug 24, 2026
rasapala
approved these changes
Aug 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MediapipeFactory::create() held a shared_lock on definitionsMtx for the entire duration of definition.create(), which in the graph queue path blocks on getIdleStream() waiting for a pool slot. With multiple inference threads continuously holding shared_locks, writers (createDefinition/reloadDefinition) needing unique_lock were starved indefinitely.
Fix: unlock definitionsMtx before calling definition.create(), since the map lookup is already complete and the definition object is kept alive independently.
Issue was detected by observing long execution times of our stress tests.
JIRA:CVS-193242