Python HTTP 599: Connection closed (Tornado)

sharkbait picture sharkbait · Apr 5, 2013 · Viewed 9.3k times · Source

I'm getting this error:

HTTP 599: Connection closed [E 130405 11:43:14 web:1031] Uncaught exception GET /networks/1/sensors/1/alarm (127.0.0.1)

while executing the following code:

from tornado.stack_context import ExceptionStackContext

def handle_exc(*args):
print('Exception occured')
return True

@tornado.gen.engine
def check_status_changes(netid, sensid):

    como_url = "".join(['http://131.114.52:44444/ztc?netid=', str(netid), '&sensid=', str(sensid), '&start=-5s&end=-1s'])

    http_client = AsyncHTTPClient()
    response = yield tornado.gen.Task(http_client.fetch, como_url)

    if response.error is not None:
            print("Terzo")
            print response.error
            with ExceptionStackContext(handle_exc):
                response.rethrow()
    else:
        print('Handle request')

    for line in response.body.split("\n"):
                if line != "": 
                    #net = int(line.split(" ")[1])
                    #sens = int(line.split(" ")[2])
                    #stype = int(line.split(" ")[3])
                    value = int(line.split(" ")[4])
                    print value
                    yield value
                    return


class AlarmHandler(BaseHandler):
    @tornado.web.authenticated
    @tornado.web.asynchronous
    @tornado.gen.engine
    def get(self, netid, sensid):
        self.lock_tables("read", ['devices'])
        status = self.db.get("SELECT status from devices \
                          WHERE id=%s AND network_id=%s", sensid, netid)
        print("Primo")
        print status

        try:
            periodic = tornado.ioloop.PeriodicCallback(check_status_changes(netid, sensid), 5000)
            value = periodic.start()
            print("Secondo")
            print value
        except:
            print("Quarto")
            periodic.stop()
            self.finish()
            return
        if value != status['status']:

            self.lock_tables("write", ['devices'])
            self.db.execute("UPDATE devices SET status=%s \
                             WHERE id=%s AND network_id=%s", value, netid, sensid)
            self.unlock_tables()
            self.notice("Status changed")

In the class AlarmHandler there is a periodic routine called check_status_changes. In this function, I obtain the error of the title when there's a response.error.

How can I set if error condition in order to return to the class and manage the situation? Thank you.

OTHER INFORMATIONS

If I do a screen of Tornado, I see this:

Primo

{'status': None}

Secondo

None

Terzo

HTTP 599: Connection closed

Exception occured

so, I think the program close the connection before the exception is rethtow!

And in the html consolle I see this error:

File "./wsn.py", line 226, in check_status_changes for line in response.body.split("\n"): AttributeError: 'NoneType' object has no attribute 'split'

Answer

MattH picture MattH · Apr 5, 2013

You are raising an exception:

if response.error:
        print("Terzo")
        print response.error
        raise Exception(response.error)
        return

This will be an Uncaught Exception. You should code this to handle the exception without raising an exception. E.g. log the error message and abort the database update.