alliedvision / VmbPy

Python API of the Vimba X SDK
BSD 2-Clause "Simplified" License
16 stars 6 forks source link

Segmentation fault when using Threading #26

Open aaaaaaaaaron opened 2 months ago

aaaaaaaaaron commented 2 months ago

I want to stream a single camera in one thread. When I close the stream and join my thread my code crashes with a segmentation fault.

I have reduced my code down to this to replicate the issue

import threading

import time
from vmbpy import *
import faulthandler

faulthandler.enable()

def start(stop_event):
    print("Initializing")
    camera_loop(stop_event)
    print("done")

def initiate_cam(cam):
    cam.LineSelector.set('Line0')
    cam.LineMode.set('Input')
    cam.TriggerSelector.set('FrameStart')
    cam.TriggerSource.set('Line0')
    cam.TriggerActivation.set('RisingEdge')
    cam.AcquisitionMode.set("Continuous")
    cam.TriggerMode.set('On')

def camera_loop(stop_event):
    with VmbSystem.get_instance() as vmb:
        cams = vmb.get_all_cameras()
        with cams[0] as cam:
            initiate_cam(cam)
            try:
                cam.start_streaming(handler=handler, buffer_count=1,
                                    allocation_mode=AllocationMode.AnnounceFrame)
                print("ready for photos...")
                stop_event.wait()
            finally:
                print("finishing")
                cam.stop_streaming()

def handler(cam: Camera, stream: Stream, frame: Frame):
    print('{} acquired {}'.format(cam, frame), flush=True)
    # do stuff
    cam.queue_frame(frame)

if __name__ == "__main__":
    producers_lock = threading.Lock()

    stop_event = threading.Event()

    with producers_lock:
        bottle_thread = threading.Thread(target=start, args=(stop_event,))
        bottle_thread.start()
    print("started")
    time.sleep(10)
    print("ending")
    with producers_lock:
        stop_event.set()
        bottle_thread.join()

The code runs fine, but when it stops it causes a segmentation fault. I feel like I am doing everything that was in the multithreading_opencv.py example but I keep getting this segmentation fault. Is this a bug? Or am I missing something from that example that would lead to a graceful shutdown?

Thank you!

Teresa-AlliedVision commented 2 months ago

I could not reproduce the issue with your code example on Windows, can you post debgging/error message and more info on your system/OS, versioning of any used modules and Python version?

aaaaaaaaaron commented 2 months ago

Thank you for testing that out. Here is my output:

Initializing
started
ready for photos...
ending
finishing
Fatal Python error: Segmentation fault

Thread 0x0000ffffbb9b4010 (most recent call first):
  File "/usr/lib/python3.9/logging/__init__.py", line 2142 in shutdown
done

The line File "/usr/lib/python3.9/logging/__init__.py", line 2142 in shutdown changes every time I run it so I don't think that is the issue.

I am using Python 3.9 on an Nvidia Jetson Orin Nano Developer Kit running Jetpack 5.1.2 (Ubuntu 20.04.6 LTS, arm64). Using my same venv I am actually able to run your multithreading example.