Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cancel logic during SessionPool.acquire() #537

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tests/aio/test_session_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ async def test_waiter_is_notified(driver):
@pytest.mark.asyncio
async def test_no_race_after_future_cancel(driver):
pool = ydb.aio.SessionPool(driver, 1)

s = await pool.acquire()
waiter = asyncio.ensure_future(pool.acquire())
await asyncio.sleep(0)
waiter.cancel()
await pool.release(s)
await asyncio.wait([waiter])

assert pool._active_queue.qsize() == 1
s = await pool.acquire()
assert s.initialized()
await pool.stop()
Expand Down
9 changes: 8 additions & 1 deletion ydb/aio/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,14 @@ async def _prepare_session(self, timeout, retry_num) -> ydb.ISession:
async def _get_session_from_queue(self, timeout: float):
task_wait = asyncio.ensure_future(asyncio.wait_for(self._active_queue.get(), timeout=timeout))
task_should_stop = asyncio.ensure_future(self._should_stop.wait())
done, _ = await asyncio.wait((task_wait, task_should_stop), return_when=asyncio.FIRST_COMPLETED)
try:
done, _ = await asyncio.wait((task_wait, task_should_stop), return_when=asyncio.FIRST_COMPLETED)
except asyncio.CancelledError:
cancelled = task_wait.cancel()
if not cancelled:
priority, session = task_wait.result()
self._active_queue.put_nowait((priority, session))
raise
if task_should_stop in done:
task_wait.cancel()
return self._create()
Expand Down
Loading