Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cuvid_264 decode conflict with other process #1471

Closed
4 tasks done
namogg opened this issue Jul 25, 2024 · 1 comment
Closed
4 tasks done

cuvid_264 decode conflict with other process #1471

namogg opened this issue Jul 25, 2024 · 1 comment
Labels

Comments

@namogg
Copy link

namogg commented Jul 25, 2024

Overview

Thread A running hardware decoder with:
codec = av.codec.CodecContext.create("h264_cuvid", 'r')
Child Process B run cuda.init():

import pycuda.driver as cuda
cuda.init()
cuda_context_0 = cuda.Device(0).make_context()

My code was working fine with h264 but h264_cuvid cause conflict.

Expected behavior

Both process and thread doesnt confict

Actual behavior

Both the thread and the process is corrupted.
Decode thread error:
[Pack decode ] An exception occurred: [Errno 542398533] Generic error in an external library
The other process got error:
pycuda._driver.LogicError: cuInit failed: initialization error

Reproduction

import threading
import multiprocessing
import pycuda.driver as cuda
import av
import numpy as np
import time
import random

def cuda_task():
    try:
        cuda.init()
        print("CUDA initialized in process")
        # Keep the process running
        while True:
            time.sleep(1)
    except Exception as e:
        print(f"[Process] CUDA init error: {e}")

def generate_random_h264_packet():
    # Generate random bytes to simulate an H.264 packet
    packet_size = random.randint(100, 1000)  # Packet size between 100 and 1000 bytes
    return bytearray(random.getrandbits(8) for _ in range(packet_size))

def decode_task():
    try:
        codec = av.codec.CodecContext.create("h264_cuvid", 'r')
        print("Decoder initialized")
        
        while True:
            print("Decoding")
            # Generate random H.264 packet
            packet_data = generate_random_h264_packet()
            packet = av.packet.Packet(packet_data)

            # Decode the packet
            frames = codec.decode(packet)

            
            
            time.sleep(1)  # Simulate some processing delay
    except Exception as e:
        print(f"[Thread] Decoding error: {e}")

if __name__ == "__main__":
    # Using threading for decoding task
    thread = threading.Thread(target=decode_task, daemon=True)
    thread.start()

    # Using multiprocessing for CUDA task
    process = multiprocessing.Process(target=cuda_task)
    process.start()

    # Keep the main process running
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        print("Main process interrupted")
        process.terminate()
        process.join()

Investigation

Starting the cuda.init() process first adn keep running doesnt cause error for both side.
Running both in process doesnt cause error

Versions

  • OS: Linux
  • PyAV runtime:
ai_dev@aidev:~$ python3 -m av --version
PyAV v12.3.0
library configuration: --prefix=/usr --extra-version=0ubuntu0.22.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-pocketsphinx --enable-librsvg --enable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
library license: GPL version 2 or later
libavcodec     58.134.100
libavdevice    58. 13.100
libavfilter     7.110.100
libavformat    58. 76.100
libavutil      56. 70.100
libswresample   3.  9.100
libswscale      5.  9.100

Research

I have done the following:

@namogg namogg added the build label Jul 25, 2024
@namogg
Copy link
Author

namogg commented Jul 26, 2024

Changing process from fork to spawn does fix the problem

@namogg namogg closed this as completed Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant