Skip to content

Commit

Permalink
Cancel current DB operation on asyncio timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Aug 28, 2015
1 parent cf5c7a3 commit 8ca948e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ CHANGES

* Support pools with unlimited size #59

* Cancel current DB operation on asyncio timeout


0.7.0 (2015-04-22)
^^^^^^^^^^^^^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion aiopg/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ def cancel(self, timeout=None):
self._conn.cancel()
if timeout is None:
timeout = self._timeout
yield from self._poll(waiter, timeout)
try:
yield from self._poll(waiter, timeout)
except psycopg2.extensions.QueryCanceledError:
pass

@asyncio.coroutine
def reset(self):
Expand Down
6 changes: 5 additions & 1 deletion aiopg/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,12 @@ def execute(self, operation, parameters=None, *, timeout=None):
except:
self._conn._waiter = None
raise
else:
try:
yield from self._conn._poll(waiter, timeout)
except asyncio.TimeoutError:
yield from self._conn.cancel()
self._impl.close()
raise

@asyncio.coroutine
def executemany(self, operation, seq_of_parameters):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,18 @@ def go():
self.assertEqual('hello', val.payload)

self.loop.run_until_complete(go())

def test_close_cursor_on_timeout_error(self):
@asyncio.coroutine
def go():
conn = yield from self.connect()
cur = yield from conn.cursor(timeout=0.01)
with self.assertRaises(asyncio.TimeoutError):
yield from cur.execute("SELECT pg_sleep(10)")

self.assertTrue(cur.closed)
self.assertFalse(conn.closed)

yield from conn.close()

self.loop.run_until_complete(go())

0 comments on commit 8ca948e

Please sign in to comment.