opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
75.69k stars 55.61k forks source link

Fix race condition in InternalFFMpegRegister initialization. #25419

Closed vrabaud closed 2 weeks ago

vrabaud commented 2 weeks ago

initLogger_ does not check if the logger has been initizalized before and it might initialize it several times from different threads, racing with other threads that are logging.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

vrabaud commented 2 weeks ago

FYI, the stack trace we have is (line numbers might be off by a few units; if needed I can get the exact code references):

  Write of size 4 at 0x556a7df5e66c by thread T152 (mutexes: write M0):
    #0 av_log_set_level third_party/ffmpeg/src/libavutil/log.c:387:18 
    #1 initLogger_ third_party/OpenCV/public/modules/./videoio/src/cap_ffmpeg_impl.hpp:950:13
    #2 InternalFFMpegRegister::init(bool) third_party/OpenCV/public/modules/./videoio/src/cap_ffmpeg_impl.hpp:930:9
    #3 CvVideoWriter_FFMPEG::open(char const*, int, double, int, int, cv::VideoWriterParameters const&) third_party/OpenCV/public/modules/./videoio/src/cap_ffmpeg_impl.hpp:2779:5
    #4 cvCreateVideoWriterWithParams_FFMPEG third_party/OpenCV/public/modules/./videoio/src/cap_ffmpeg_impl.hpp:3344:17
...
  Previous read of size 4 at 0x556a7df5e66c by thread T190 (mutexes: write M1):
    #0 av_log_default_callback third_party/ffmpeg/src/libavutil/log.c:316:17
    #1 av_vlog third_party/ffmpeg/src/libavutil/log.c:377:9
    #2 av_log third_party/ffmpeg/src/libavutil/log.c:369:5
    #3 h264_parse_nal_header third_party/ffmpeg/src/libavcodec/h2645_parse.c:324:5