Skip to content

Fix async query pool acquire race condition #570

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

Merged
merged 1 commit into from
Mar 7, 2025
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
17 changes: 17 additions & 0 deletions tests/aio/query/test_query_session_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,20 @@ async def test_no_session_leak(self, driver, docker_project):

docker_project.start()
await pool.stop()

@pytest.mark.asyncio
async def test_acquire_no_race_condition(self, driver):
ids = set()
async with ydb.aio.QuerySessionPool(driver, 1) as pool:

async def acquire_session():
session = await pool.acquire()
ids.add(session._state.session_id)
await pool.release(session)

tasks = [acquire_session() for _ in range(10)]

await asyncio.gather(*tasks)

assert len(ids) == 1
assert pool._current_size == 1
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ deps =

[testenv:py-proto5]
commands =
pytest -v -m "not tls" --docker-compose-remove-volumes --docker-compose=docker-compose.yml {posargs} --ignore=tests/topics
pytest -v -m "not tls" --docker-compose-remove-volumes --docker-compose=docker-compose.yml {posargs}
deps =
-r{toxinidir}/test-requirements.txt
protobuf<6.0.0

[testenv:py-proto4]
commands =
pytest -v -m "not tls" --docker-compose-remove-volumes --docker-compose=docker-compose.yml {posargs} --ignore=tests/topics
pytest -v -m "not tls" --docker-compose-remove-volumes --docker-compose=docker-compose.yml {posargs}
deps =
-r{toxinidir}/test-requirements.txt
protobuf<5.0.0
Expand All @@ -55,7 +55,7 @@ deps =

[testenv:py-proto3]
commands =
pytest -v -m "not tls" --docker-compose-remove-volumes --docker-compose=docker-compose.yml {posargs} --ignore=tests/topics
pytest -v -m "not tls" --docker-compose-remove-volumes --docker-compose=docker-compose.yml {posargs}
deps =
-r{toxinidir}/test-requirements.txt
protobuf<4.0.0
Expand Down
9 changes: 8 additions & 1 deletion ydb/aio/query/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,15 @@ async def acquire(self) -> QuerySession:
logger.debug(f"Acquired dead session from queue: {session._state.session_id}")

logger.debug(f"Session pool is not large enough: {self._current_size} < {self._size}, will create new one.")
session = await self._create_new_session()

self._current_size += 1
try:
session = await self._create_new_session()
except Exception as e:
logger.error("Failed to create new session")
self._current_size -= 1
raise e

return session

async def release(self, session: QuerySession) -> None:
Expand Down
Loading