# HG changeset patch # User wbell # Date 1185228187 0 # Node ID 79262e20b073d1264fca16960290dd6d2e4cb3d4 # Parent 446092a2d2fe68bac779e8e29dd6d0dd25256083 Protect the main beep loop from exceptions-- either problems in callbacks or errors in the asyncore handlers, which can stop the scheduled events from getting run. Also move over to using the asyncore.loop function over the poll function-- using poll as we were seeing use_poll=False (the default) hang forever, making scheduled events not get run. diff --git a/bitten/util/beep.py b/bitten/util/beep.py --- a/bitten/util/beep.py +++ b/bitten/util/beep.py @@ -122,17 +122,28 @@ granularity = timedelta(seconds=granularity) socket_map = asyncore.socket_map last_event_check = datetime.min - while socket_map: - now = datetime.now() - if now - last_event_check >= granularity: - last_event_check = now - while self.eventqueue: - when, callback = heappop(self.eventqueue) - if now < when: - heappush(self.eventqueue, (when, callback)) - break - callback() - asyncore.poll(timeout) + try: + while socket_map: + now = datetime.now() + if now - last_event_check >= granularity: + last_event_check = now + while self.eventqueue: + when, callback = heappop(self.eventqueue) + if now < when: + heappush(self.eventqueue, (when, callback)) + log.debug('Checking done %d events.', len(self.eventqueue)) + break + try: + callback() + except: + log.error('Exception caught firing callback %s. Ignoring.', callback.__name__) + try: + asyncore.loop(timeout, True, None, 1) + except: + log.error('Exception caught in asyncore.loop, ignoring.'); + except: + log.error('Exception caught in run()'); + def schedule(self, delta, callback): """Schedule a function to be called.