charmedlabs / pixy2

Other
131 stars 98 forks source link

Segmentation fault Python3 #20

Open Corteil opened 4 years ago

Corteil commented 4 years ago

I'm trying to trap a 'Segmentation fault' when the Pixy Camera is not connected or being used by another program. 'Segmentation fault' is printed to the command line and program is exited. The program is in Python3.

import pixy
from pixy import *

pixy.init()
pixy.change_prog("line")

fails at line 4, if another program or PixyMon is running.

richlegrand commented 4 years ago

Hi Corteil, Yes, this is indeed an issue that needs to be fixed. Let me know if you find anything useful. It's on our bug list.

thanks!

whaleygeek commented 4 years ago

@richlegrand @Corteil I dug into the code for this and libusb init looks fine.

I had a hunch that SWIG is not configured to generate an exception wrapper for the init.

I suggested to Brian on twitter to remove the change_prog() line and see if the Seg Fault goes away, on the hunch that it is (incorrectly) trying to access a driver that was not initialised after it found no hardware or the hardware was locked and therefore a context and handle for it could not be obtained in the C lib. Due to lack of state checks in the change_prog() C function this would cause access to an invalid memory location (via a zero context variable, effectively NULL).

Brian removed the last line and the SIGSEGV doesn't happen, as I predicted.

The user fix is therefore to check the return result from pixy.init() and if it is non zero, the hardware is locked or not present. His user program can then take evasive action.

The library fix here is either (a) change the SWIG configuration to generate an exception wrapper for the init() call and throw an exception if the return result is non zero, or (b) all methods in the C pixy module in the library need to be modified to be defensive, and check if the init worked or not (probably by validating m_context or m_handle).

Twitter chat is here: https://twitter.com/whaleygeek/status/1232384123840475139?s=20

Hope this helps.

whaleygeek commented 4 years ago

@richlegrand You might want to consider hosting this on pypi.org and configuring a wheel for this, so that host users can pip install it without needing to build all the code. (The wheels will pre-compile for all configured platforms for you, so users don't have to build it themselves.

Wheels are documented here: https://pythonwheels.com/

There is also a PiWheels project now, as your package will be a popular thing on RaspberryPi especially with the PiWars robotics competition, and this would allow Raspberry Pi users to just pip install the package and get the libs ready-built.

https://www.piwheels.org/

richlegrand commented 4 years ago

Ah, thank you Brian. That is very helpful!

plauche commented 4 years ago

Is this still an existing issue?