Skip to content

docs: add multiprocessing fork safety warning (fixes #1166)#1225

Open
molhamfetnah wants to merge 2 commits intoopencv:4.xfrom
molhamfetnah:fix/1166-corrupted-double-linked-list
Open

docs: add multiprocessing fork safety warning (fixes #1166)#1225
molhamfetnah wants to merge 2 commits intoopencv:4.xfrom
molhamfetnah:fix/1166-corrupted-double-linked-list

Conversation

@molhamfetnah
Copy link
Copy Markdown

Summary

  • Add documentation about OpenCV not being fork-safe in the cv2 module docstring
  • Documents the "corrupted double-linked list" issue that occurs when using cv2 functions after fork()
  • Provides recommended workarounds: spawn mode or cv2.setNumThreads(0)

Technical Context

This is a known limitation of OpenCV, not a bug in the Python bindings. The "corrupted double-linked list" error is caused by:

  1. OpenCV's internal memory allocator and thread pool state not surviving fork()
  2. When a child process inherits the parent's OpenCV state, internal data structures can become corrupted
  3. This manifests as heap corruption errors when using cv2 functions in the child

The root fix would require adding pthread_atfork handlers in OpenCV's C++ core (opencv/opencv) to reset internal state on fork. This packaging-only repo cannot implement that fix.

Workarounds (documented)

Users should either:

  1. Use spawn mode: multiprocessing.set_start_method('spawn')
  2. Disable OpenCV threads in workers: cv2.setNumThreads(0) before using cv2

This is the same workaround recommended in other related issues (#5150, #1007).

Related Issues

- Changed install_requires to specify minimum numpy versions per Python version:
  - Python 3.9-3.12: numpy>=2.0.2
  - Python 3.13: numpy>=2.1.3
  - Python 3.14+: numpy>=2.3.0
- This fixes NumPy 2.x ABI compatibility issue where wheels
  compiled against NumPy 1.x fail at runtime with NumPy 2.x

Fixes opencv#1201
Add documentation about OpenCV not being fork-safe and the
'corrupted double-linked list' issue that can occur when using
cv2 functions after fork().

Fixes: opencv#1166
Copilot AI review requested due to automatic review settings May 9, 2026 18:02
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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