Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1f2c466

Browse files
authoredJan 13, 2025··
Merge pull request #537 from MBogda/fix-cancel-during-session-pool-wait
Fix cancel logic during SessionPool.acquire()
2 parents cf0c906 + 60a4d87 commit 1f2c466

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed
 

‎tests/aio/test_session_pool.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ async def test_waiter_is_notified(driver):
3434
@pytest.mark.asyncio
3535
async def test_no_race_after_future_cancel(driver):
3636
pool = ydb.aio.SessionPool(driver, 1)
37+
3738
s = await pool.acquire()
3839
waiter = asyncio.ensure_future(pool.acquire())
40+
await asyncio.sleep(0)
3941
waiter.cancel()
4042
await pool.release(s)
43+
await asyncio.wait([waiter])
44+
45+
assert pool._active_queue.qsize() == 1
4146
s = await pool.acquire()
4247
assert s.initialized()
4348
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:
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
567574
if task_should_stop in done:
568575
task_wait.cancel()
569576
return self._create()

0 commit comments

Comments
 (0)
Please sign in to comment.