fix: add thread safety to JPEG2000 decode#1226
Closed
molhamfetnah wants to merge 3 commits intoopencv:4.xfrom
Closed
fix: add thread safety to JPEG2000 decode#1226molhamfetnah wants to merge 3 commits intoopencv:4.xfrom
molhamfetnah wants to merge 3 commits intoopencv:4.xfrom
Conversation
- 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
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.
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 withAutoLockto serialize access:Jpeg2KOpjDecoderBase::readHeader()- locks during header parsingJpeg2KOpjDecoderBase::readData()- locks during image decodingJpeg2KOpjEncoder::write()- locks during encodingThis ensures thread-safe access to the OpenJPEG library across all decode/encode operations.
Related