Skip to content

Commit

Permalink
Make resiliant to callbacks throwing execeptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ocalvo authored and nijel committed Jul 10, 2020
1 parent a1e52b9 commit 0cfd632
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
5 changes: 5 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
3.1
===

* Fix an issue where the gammu worker thread could be brought down if a callback throws an exception

3.0
===

Expand Down
15 changes: 11 additions & 4 deletions gammu/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,14 @@ def get_name(self):
'''
return self._name

def gammu_pull_device(state_machine):
state_machine.ReadDevice()

class GammuThread(threading.Thread):
'''
Thread for phone communication.
'''
def __init__(self, queue, config, callback):
def __init__(self, queue, config, callback, pull_func = gammu_pull_device):
'''
Initialises thread data.
Expand All @@ -180,6 +182,7 @@ def __init__(self, queue, config, callback):
self._callback = callback
self._queue = queue
self._sm.SetConfig(0, config)
self._pull_func = pull_func

def _do_command(self, name, cmd, params, percentage=100):
'''
Expand Down Expand Up @@ -234,7 +237,10 @@ def run(self):
if self._terminate:
break
# Read the device to catch possible incoming events
self._sm.ReadDevice()
try:
self._poll_func(self._sm)
except Exception as ex:
self._callback("ReadDevice", None, ex, 0)

def kill(self):
'''
Expand All @@ -257,7 +263,7 @@ class GammuWorker(object):
done, caller is notified via callback.
'''

def __init__(self, callback):
def __init__(self, callback, pull_func = gammu_pull_device):
'''
Initializes worker class.
Expand All @@ -268,6 +274,7 @@ def __init__(self, callback):
self._config = {}
self._lock = threading.Lock()
self._queue = queue.Queue()
self._pull_func = pull_func

def enqueue_command(self, command, params):
'''
Expand Down Expand Up @@ -331,7 +338,7 @@ def initiate(self):
'''
Connects to phone.
'''
self._thread = GammuThread(self._queue, self._config, self._callback)
self._thread = GammuThread(self._queue, self._config, self._callback, self._pull_func)
self._thread.start()

def terminate(self, timeout=None):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import sys

# some defines
VERSION = '3.0'
VERSION = '3.1'
GAMMU_REQUIRED = '1.37.90'
README_FILE = os.path.join(os.path.dirname(__file__), 'README.rst')
with codecs.open(README_FILE, 'r', 'utf-8') as readme:
Expand Down

0 comments on commit 0cfd632

Please sign in to comment.