3

I cannot understand why sometimes I cannot catch the TimeOutError inside my flash_serial_buffer method.

When running my program I sometimes get a TimeOutError that is not caught and I cannot understand why. I indicate the code of the signal handlers and the methods where the TimeOutError is not caught. How could this be happening?

This is the code for my signal handler definition and callback function.

Basically if the time ends, the signal handler is called and raises a timeout error.

def signal_handler(signum, frame):
    print "PUM"
    raise TimedOutError("Time out Error")

signal.signal(signal.SIGALRM, signal_handler)

The flush serial buffer blocks if there is no answer to

answer = xbee.wait_read_frame()

The idea is to clean everything in the buffer until there aren’t any more messages. When there are no more messages, it just waits for the SIGALRM to explode and raises the timeout error.

def flush_serial_buffer(xbee):

    # Flush coordinators serial buffer if problem happened before
    logging.info("     Flashing serial buffer")
    try:
        signal.alarm(1)  # Seconds
        while True:
            answer = xbee.wait_read_frame()
            signal.alarm(1)
            logging.error("    Mixed messages in buffer")
    except TimedOutError:
        signal.alarm(0)  # Seconds
        logging.error("    No more messages in buffer")

    signal.alarm(0) # Supposedly it never leaves without using Except, but...

Is there a case where the TimeOutError might be raised, but not caught by the try: statement?

Here is my error class definition:

class TimedOutError(Exception):
pass

I was able to repeat the error again. I really cannot understand why the try does not catch the error it.

INFO:root:     Flashing serial buffer
PUM
Traceback (most recent call last):
  File "/home/ls/bin/pycharm-community-4.0.6/helpers/pydev/pydevd.py", line 1458, in trace_dispatch
    if self._finishDebuggingSession and not self._terminationEventSent:
  File "/home/ls/PiProjects/Deployeth/HW-RPI-API/devices.py", line 42, in signal_handler
    raise TimedOutError("Time out Error")
TimedOutError: Time out Error
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LPS
  • 375
  • 3
  • 16
  • 2
    Is it because the exception is called [`TimeoutException`](https://github.com/thom-nic/python-xbee/blob/master/xbee/base.py#L24)? – wflynny Oct 21 '15 at 21:40
  • I took a closer look at what you said @wflynny, shouldnt that TimeoutException be caught inside the Xbee module? – LPS Oct 21 '15 at 21:49
  • @wflynny i am importing the Xbee module. The TimedOutError class i am talking about is defined by me in my own module, and that is the exception i am not cathcing. I am waiting to be able to repeat the error to paste it here. – LPS Oct 21 '15 at 22:57
  • your question before the edits (and still now) is hard to interpret because in the text you claim the error is `TimeOutError` but in the code I only see `TimedOutError` (with a **d**). And originally, the way the question was written, it was unclear where the exception was defined -- it was perfectly reasonable to assume you did something like `from xbee import *`. – wflynny Oct 22 '15 at 00:58
  • @wflynny do you think i should use the `TimeOutException` from the xbee module instead of using my own alarm with the `TimedOutError` attached to solve the, non-block while reading, issue? – LPS Oct 22 '15 at 09:27
  • Looks like the `signal_handler` is run in a different thread than your `try..catch` block. I'm not familiar with `serial`, so this might be wildly wrong, but the fact that exception is not caught and the traceback you posted are consistent with the theory. – J0HN Oct 23 '15 at 09:54
  • @J0HN The `signal_handler` and the `signal.signal(signal.SIGALRM, signal_handler) ` are being called inside the same class that contains the method `flush_serial_buffer`. Do you think that they should be referenced in the root of the module instead of inside the class ? Do you think that could create an issue ? – LPS Oct 23 '15 at 13:44
  • @J0HN Relatively to the Thread issue they say that if the [signal](https://docs.python.org/2/library/signal.html) is used in the same thread where it is created, there should be no problem. – LPS Oct 23 '15 at 13:51

1 Answers1

0

I would recommend in this case replacing the try and except code with this:

try:
    signal.alarm(1)  # Seconds
    while True:
        answer = xbee.wait_read_frame()
        signal.alarm(1)
        logging.error("    Mixed messages in buffer")
except:
    signal.alarm(0)  # Seconds
    logging.error("    No more messages in buffer")

PS: You don't need to include try (whatever error) in your try and except statements.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131