put results.close() in finally block

This commit is contained in:
Noah Levitt 2015-09-16 19:02:59 +00:00
parent b093c05a4a
commit 4ce4c4f0f6

View File

@ -39,15 +39,23 @@ class Rethinker:
iterating over the results, for proper support of cursors, which fetch iterating over the results, for proper support of cursors, which fetch
from the server more than once.""" from the server more than once."""
success = False success = False
while not success: results = None
with self._random_server_connection() as conn: try:
try: while not success:
results = query.run(conn, db=self.db) with self._random_server_connection() as conn:
success = True try:
for result in results: results = query.run(conn, db=self.db)
yield result success = True
if hasattr(results, 'close'): for result in results:
results.close() yield result
except (r.ReqlAvailabilityError, r.ReqlTimeoutError) as e: except (r.ReqlAvailabilityError, r.ReqlTimeoutError) as e:
self.logger.error('will retry rethinkdb query/operation %s which failed like so:', exc_info=True) 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()