heynemann / motorengine

Motorengine is a port of MongoEngine for Tornado.
http://motorengine.readthedocs.org
204 stars 67 forks source link

Not raising any Exception when there is not connection #122

Open rubenbonilla opened 8 years ago

rubenbonilla commented 8 years ago

Python:

Libraries:

I'm doing some tests with this library and something that i can't handle is when i'm trying to do any request to MongoDB when it's down.

This warning is shown on the console: [W 160714 09:55:26 iostream:1186] Connect error on fd 14: ECONNREFUSED

When this message appears in a few seconds the RAM is overloaded blocking the computer and i just must wait to try to kill the process.

Im triying to handle this when i instanciate the connection:

io_loop = tornado.ioloop.IOLoop.instance()
    try:
        mongo_connection = connect("apitest", io_loop=io_loop)
        print dir(mongo_connection)
        print type(mongo_connection)
        print mongo_connection.connection
        print mongo_connection.previous_error().result
    except ConnectionError as err:
        print err.message
    except Exception as err:
        print err.message

    application_apiEngine = tornado.web.Application(routes)
    application_apiEngine.listen(options.port)
    io_loop.start()

and also in the callback when i create a document:

@gen.coroutine
    def create(self, data):
        doc = Doc(data)
        try:
            result = doc.save()
        except Exception as err:
            print 'i'm Error'
            raise gen.Return(False)
        else:
            raise gen.Return(True)

I just want to be sure if in some case the connection is lost i will get a feedback for just send some error messge in my service.

rubenbonilla commented 8 years ago

I was doing a debug on the library. I'm pretty sure that the bug is happening on connection.py file on line 100.

 try:
         if not _connections[alias].connected:
            _connections[alias].open_sync()

    except Exception:
        exc_info = sys.exc_info()
        err = ConnectionError("Cannot connect to database %s :\n%s" % (alias, exc_info[1]))
        raise six.reraise(ConnectionError, err, exc_info[2])

When if not _connections[alias].connected is asked it always returns a dict with data like: MotorDatabase(Database(MongoClient([]), u'connected')) so always is gonna be true, always connected, never raises an error so you can't handled when the connection is lost.