fix: address unit-of-work verify findings

Abort the transport when a bounded close ends, so a frozen server leaves
no sessions over the pool once the ping has no transaction. Under the
shared request deadline a block sets no timer of its own, so a single
cancel closes the connection and the log has one record. Add a test that
concurrent worker deliveries get separate units of work, and state the
cost of the pattern against both baselines in the report.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-01 01:58:51 +03:00
co-authored by Claude Opus 5.5
parent 952c16350e
commit d8fe2e87f1
17 changed files with 588 additions and 60 deletions
+13 -3
View File
@@ -28,13 +28,23 @@ class BoundedCloseConnection(asyncpg.Connection):
соединение для CancelRequest и в `close()` ждёт его без срока.
SQLAlchemy при отмене задачи закрывает соединение мягко, поэтому
замёрзший Postgres держал бы запрос до разморозки. Здесь всё
закрытие ограничено: по истечении asyncpg обрывает соединение, а
SQLAlchemy переходит к принудительному закрытию.
закрытие ограничено, а транспорт обрывается в `finally`: прерванный
`close()` asyncpg уже пометил протокол закрывающимся, и его `abort()`
выходит сразу, не трогая сокет. Без этого сокет открыт до конца процесса,
и после разморозки сессия PostgreSQL живёт сверх пула, а срок простоя
транзакции её не снимет, если транзакции нет (проверка `_ping_once`).
После штатного закрытия обрыв ничего не делает: Terminate отправлен, и
транспорт asyncpg уже закрыл сам. `_transport` — внутреннее имя asyncpg,
его смену ловит `test_no_sessions_over_pool_after_freeze`.
"""
async def close(self, *, timeout: float | None = None) -> None:
limit = timeout or DB_CLOSE_TIMEOUT_SECONDS
await asyncio.wait_for(super().close(timeout=timeout), limit)
transport = self._transport
try:
await asyncio.wait_for(super().close(timeout=timeout), limit)
finally:
transport.abort()
def _ping_once(dbapi_connection: Any) -> bool: