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,6 +39,8 @@ 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
results = None
try:
while not success: while not success:
with self._random_server_connection() as conn: with self._random_server_connection() as conn:
try: try:
@ -46,8 +48,14 @@ class Rethinker:
success = True success = True
for result in results: for result in results:
yield result yield result
if hasattr(results, 'close'):
results.close()
except (r.ReqlAvailabilityError, r.ReqlTimeoutError) as e: 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) 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()