Skip to content

Serialize concurrent calls to shared native face detectors - #1756

Merged
serengil merged 2 commits into
serengil:masterfrom
Afloat16:fix/mediapipe-graph-concurrency-20261116
Nov 16, 2026
Merged

serengil merged 2 commits into
serengil:masterfrom
Afloat16:fix/mediapipe-graph-concurrency-20261116

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Tickets

N/A

What has been done

Cached detector instances are shared across requests, but MediaPipe graphs and OpenCV Haar classifiers keep mutable native inference state. Overlapping calls can mix detections, raise packet-timestamp errors, or corrupt the classifier's image buffers.

Serialize MediaPipe process calls and OpenCV face/eye classification calls with a lock owned by each detector instance. Separate instances can still run concurrently, and native errors release the lock. Update existing detector fixtures to use their constructors so their inference locks are initialized.

How to test

  • python -m pytest -q tests/unit/test_mediapipe_concurrent_inference.py tests/unit/test_mediapipe_color_order.py tests/unit/test_opencv_concurrent_inference.py tests/unit/test_face_crop_bounds.py tests/unit/test_ssd_concurrent_inference.py tests/unit/test_ssd_opencv_version.py
  • Python 3.10 and 3.12 validation: 5 regressions fail and 8 controls pass on the base revision; 55 tests pass after the fix, including real MediaPipe 0.10.21 short-range and full-range graph checks.
  • Real OpenCV 4.11 validation uses six worker threads and compares every result against the same image's sequential result. After the fix, all 90 face-detection calls and 90 eye-detection calls agree on both Python versions.
  • The separate unfixed native run recorded 55/56 incorrect face results out of 90 before its eye-detection phase terminated with a segmentation fault / invalid-pointer abort. That failed baseline run is preserved separately from the successful fixed run.
  • Pylint: 10.00/10; mypy: 145 source files pass; changed detector modules and new tests pass Black with line length 100.

@Afloat16 Afloat16 changed the title Serialize concurrent requests to shared MediaPipe graphs Serialize concurrent calls to shared native face detectors Nov 16, 2026
@serengil
serengil merged commit 5e16bdf into serengil:master Nov 16, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants