From 4ce4c4f0f666c71278c142e72de44bbbee0e0a31 Mon Sep 17 00:00:00 2001 From: Noah Levitt Date: Wed, 16 Sep 2015 19:02:59 +0000 Subject: [PATCH] put results.close() in finally block --- pyrethink/__init__.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/pyrethink/__init__.py b/pyrethink/__init__.py index 9217919..df9e5f2 100644 --- a/pyrethink/__init__.py +++ b/pyrethink/__init__.py @@ -39,15 +39,23 @@ class Rethinker: iterating over the results, for proper support of cursors, which fetch from the server more than once.""" success = False - while not success: - with self._random_server_connection() as conn: - try: - results = query.run(conn, db=self.db) - success = True - for result in results: - yield result - if hasattr(results, 'close'): - results.close() - except (r.ReqlAvailabilityError, r.ReqlTimeoutError) as e: - self.logger.error('will retry rethinkdb query/operation %s which failed like so:', exc_info=True) + results = None + try: + while not success: + with self._random_server_connection() as conn: + try: + results = query.run(conn, db=self.db) + success = True + for result in results: + yield result + except (r.ReqlAvailabilityError, r.ReqlTimeoutError) as e: + if not success: + self.logger.error('will retry rethinkdb query/operation %s which failed like so:', exc_info=True) + else: + # initial query was successful, subsequent fetch + # perhaps failed, only caller can know what to do + raise + finally: + if results and hasattr(results, 'close'): + results.close()