Skip to content

fix: add thread safety to JPEG2000 decode#1226

Closed
molhamfetnah wants to merge 3 commits intoopencv:4.xfrom
molhamfetnah:fix/1007-jpeg2000-thread-safety
Closed

fix: add thread safety to JPEG2000 decode#1226
molhamfetnah wants to merge 3 commits intoopencv:4.xfrom
molhamfetnah:fix/1007-jpeg2000-thread-safety

Conversation

@molhamfetnah
Copy link
Copy Markdown

Summary

Technical Details

The JPEG2000 decode operations (imdecode) can cause segfault or deadlock when called from multiple threads simultaneously. This is due to OpenJPEG library maintaining global state that isn't thread-safe.

The fix adds a static mutex (getOpenJpegMutex()) and wraps all OpenJPEG operations with AutoLock to serialize access:

  • Jpeg2KOpjDecoderBase::readHeader() - locks during header parsing
  • Jpeg2KOpjDecoderBase::readData() - locks during image decoding
  • Jpeg2KOpjEncoder::write() - locks during encoding

This ensures thread-safe access to the OpenJPEG library across all decode/encode operations.

Related

- 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
- Add mutex protection around OpenJPEG decode operations
- Prevent segfault/deadlock when multiple threads call imdecode simultaneously
- Apply mutex lock in readHeader, readData, and write methods
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.

@asmorkalov asmorkalov closed this May 10, 2026
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.

3 participants