Skip to content

Commit cf5f474

Browse files
committed
Cancel queue get in case of cancel of parent.
1 parent 0dfe8e7 commit cf5f474

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

tests/aio/test_session_pool.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,21 @@ async def test_waiter_is_notified(driver):
3333

3434
@pytest.mark.asyncio
3535
async def test_no_race_after_future_cancel(driver):
36+
async def first_session(pool: ydb.aio.SessionPool):
37+
s = await pool.acquire()
38+
await asyncio.sleep(0.003)
39+
await pool.release(s)
40+
41+
async def second_session(pool: ydb.aio.SessionPool):
42+
await asyncio.sleep(0.001)
43+
waiter = asyncio.ensure_future(pool.acquire())
44+
await asyncio.sleep(0.001)
45+
waiter.cancel()
46+
3647
pool = ydb.aio.SessionPool(driver, 1)
37-
s = await pool.acquire()
38-
waiter = asyncio.ensure_future(pool.acquire())
39-
waiter.cancel()
40-
await pool.release(s)
48+
await asyncio.gather(first_session(pool), second_session(pool))
49+
50+
assert pool._active_queue.qsize() == 1
4151
s = await pool.acquire()
4252
assert s.initialized()
4353
await pool.stop()

ydb/aio/table.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,14 @@ async def _prepare_session(self, timeout, retry_num) -> ydb.ISession:
563563
async def _get_session_from_queue(self, timeout: float):
564564
task_wait = asyncio.ensure_future(asyncio.wait_for(self._active_queue.get(), timeout=timeout))
565565
task_should_stop = asyncio.ensure_future(self._should_stop.wait())
566-
done, _ = await asyncio.wait((task_wait, task_should_stop), return_when=asyncio.FIRST_COMPLETED)
566+
try:
567+
done, _ = await asyncio.wait((task_wait, task_should_stop), return_when=asyncio.FIRST_COMPLETED)
568+
except asyncio.CancelledError as exc:
569+
cancelled = task_wait.cancel()
570+
if not cancelled:
571+
priority, session = task_wait.result()
572+
self._active_queue.put_nowait((priority, session))
573+
raise exc
567574
if task_should_stop in done:
568575
task_wait.cancel()
569576
return self._create()

0 commit comments

Comments
 (0)