getsenic / gatt-python

Bluetooth GATT SDK for Python
MIT License
318 stars 86 forks source link

Fix: can't handle sigint when running gatt.DeviceManager outside the mainthread #28

Open lcoudeville opened 6 years ago

lcoudeville commented 6 years ago

I discovered that it was impossible to set a signal handler for SIGINT in combination with a running gatt.DeviceManager.

Code to reproduce (pseudocode):

def main():
    def trap_sigint(*args, **kwargs):
        logging.info("SIGNAL SIGINT")

    # SIGINT (2)
    signal.signal(signal.SIGINT, trap_sigint)

    def run_gatt_devicemanager:
        import gatt
        dm =  gatt.DeviceManager()
        dm.run()

     t = Thread(target=run_gatt_devicemanager)
     t.start()
     t.join()

This code raises a KeyboardInterrupt exception when SIGINT was received which is quite unexpected as a signal handler for SIGINT was set.

Also fix subissue related to CTRL + C, which is a SIGINT in fact, of https://github.com/getsenic/gatt-python/issues/5#issuecomment-313362380 . The fix in my pull request was retrieved from https://stackoverflow.com/a/47947420 .

lcoudeville commented 5 years ago

Thank you Snevzor for your strong support I really appreciate this a lot. #staystrong #prayforgatt-python

larsblumberg commented 5 years ago

@lcoudeville Thanks for your PR! I understand that you want to tun the DeviceManager in a separate thread, however I still have troubles understanding the implications of this PR. Can you please describe in more details: