opencv / opencv/opencv-python

imdecode jpeg2000 segfault or deadlock in multithreading

Open
#1,007 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.4k
Forks
1k
Avg merge
22h 17m
Merged PRs (30d)
3

Description

Expected behaviour

In the main thread, it works well as written in the example bellow:

import multiprocessing.pool
import cv2
import numpy as np
import tqdm
 
img = np.random.randint(0, 65335, (1024, 1024), dtype=np.uint16)
_, img_data = cv2.imencode(".jp2", img)
n = 100
 
def read(i):
    image = cv2.imdecode(buffer, cv2.IMREAD_ANYDEPTH | cv2.IMREAD_GRAYSCALE)
 
list(tqdm.tqdm(map(read, range(n)), total=n, desc="main thread main process"))
with multiprocessing.pool.Pool() as pool:
   list(tqdm.tqdm(pool.imap_unordered(read, range(n)), total=n, desc="main thread child process"))
Actual behaviour

But in a thread, it is all broken!

import multiprocessing.pool
import cv2
import numpy as np
import tqdm
 
img = np.random.randint(0, 65335, (1024, 1024), dtype=np.uint16)
_, img_data = cv2.imencode(".jp2", img)
n = 100
 
def read(i):
    image = cv2.imdecode(buffer, cv2.IMREAD_ANYDEPTH | cv2.IMREAD_GRAYSCALE)
 
with multiprocessing.pool.ThreadPool() as pool:
   list(tqdm.tqdm(pool.imap_unordered(read, range(n)), total=n, desc="child thread main process"))

When the code does not freeze by itself, an interrupt with ctrl+c leads allways to a segfault. Unlike the threadless example.

Steps to reproduce
  • Tested on Debian, Ubuntu and Mint
  • Version 4.9.0 of opencv-contrib-python-headless
  • Tested on python 3.9, 3.10, 3.11 and 3.12
  • architecture x86

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied Python ThreadPool reproducer and compare it with the multiprocessing Pool case on the listed Python and Debian-based environments. Verify the buffer setup and observe whether JPEG2000 decoding completes safely under concurrent threads; done means no deadlock or segfault and successful decoding across the repeated calls.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.