opencv_contrib icon indicating copy to clipboard operation
opencv_contrib copied to clipboard

wechat_qrcode multithreading issue in global_histogram_binarizer.cpp

Open nxt007 opened this issue 2 years ago • 2 comments

System information (version)
  • OpenCV => :4.6.0:
  • Operating System / Platform => :Ubuntu 20.04 amd64:
  • Compiler => :any:
Detailed description

In file opencv_contrib-4.6.0/modules/wechat_qrcode/src/zxing/common/binarizer/global_histogram_binarizer.cpp there is global shared state in line 18, which causing segfault in case of multithreading.

Steps to reproduce

Flaky to reproduce. In my case, one of 4 test runs with 8 threads simultaneously processing same image with same WeChatQRCode instance for around 15 sec cause segfault.

Cant provide image due to NDA. Any small image like 120 * 50 should be fine, I guess.

{
    auto index = 0;
    auto image = mats[index];

    bool finish = false;
    std::atomic_uint32_t counter;
    std::vector<std::thread> threads;

    cv::WeChatQRCode decoder;
    for (size_t i = 0; i < 8; ++i) {
        threads.emplace_back([&] {
            while (!finish) {
                decoder.detectAndDecode(image, {});
                auto val = counter++;
                if (val % 100 == 0) {
                    std::cout << val << std::endl;
                }
            }
        });
    }

    while (counter < 7000) {
        sleep(1);
    }

    finish = true;
    for (auto& t : threads) {
        t.join();
    }
}
How to fix:

Wipe out this shared variable and initialize luminances var with default ctor. Also add check for !luminances in line 29.

Issue submission checklist
  • [x] I report the issue, it's not a question
  • [x] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • [x] I updated to the latest OpenCV version and the issue is still there
  • [x] There is reproducer code and related data files: videos, images, onnx, etc

nxt007 avatar Dec 28 '22 09:12 nxt007

I have the same problem

misu99 avatar Dec 27 '23 06:12 misu99

I also met this problem. The Implement of shared ArrayRef is not safe in multithreads. The Solution mentioned by @nxt007 Is Right. The global Shared ArrayRef EMPTY should not used In multithread.

YoungZH-code avatar Apr 15 '24 14:04 YoungZH-code