2 Commits
Author SHA1 Message Date
dsql 4e75a800f1 fix: connect() never orphans a pool (psql-3)
connect() now closes an existing pool before re-connecting (no orphan on double-connect),
and tears down the freshly-built pool if the SELECT-1 validation fails before re-raising
(no leaked pool on a failed connect). verified vs embedded postgres. bump v0.1.1 -> v0.1.2

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-07-01 00:28:27 -04:00
dsql 426fad10ed fix: transaction() releases the pooled connection when start() fails (psql-1)
_Transaction.__aenter__ acquired a connection then called tx.start(); if start() raised
(stale pooled conn after failover/idle-timeout — a fail-loud path), __aexit__ never ran so
the connection leaked, draining the pool until transaction() deadlocked on acquire(). now
release-on-failure. verified: 6 forced start-failures (2x pool) no longer exhaust the pool.
bump v0.1.0 -> v0.1.1

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-30 21:04:43 -04:00
3 changed files with 28 additions and 10 deletions
+3 -3
View File
@@ -10,18 +10,18 @@ a sibling of the `mongo` lib. Class is **`PsqlDB`**.
`requirements.txt`: `requirements.txt`:
``` ```
psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v0.1.0 psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v0.1.2
``` ```
Direct: Direct:
```bash ```bash
pip install "psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v0.1.0" pip install "psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v0.1.2"
``` ```
Pulls `asyncpg`. Pulls `asyncpg`.
Drop the `@v0.1.0` suffix from the line above to install the latest unpinned. Drop the `@v0.1.2` suffix from the line above to install the latest unpinned.
## The two-layer API ## The two-layer API
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project] [project]
name = "psql" name = "psql"
version = "0.1.0" version = "0.1.2"
description = "async postgres wrapper over asyncpg: two-layer API (friendly verbs + raw escape hatch), fail-loud, config-free" description = "async postgres wrapper over asyncpg: two-layer API (friendly verbs + raw escape hatch), fail-loud, config-free"
requires-python = ">=3.10" requires-python = ">=3.10"
dependencies = [ dependencies = [
+22 -4
View File
@@ -108,14 +108,24 @@ class PsqlDB:
async def connect(self) -> "PsqlDB": async def connect(self) -> "PsqlDB":
"""build the pool and validate it with SELECT 1; fail loud on bad config """build the pool and validate it with SELECT 1; fail loud on bad config
returns self so callers can write `db = await PsqlDB(...).connect()`. returns self so callers can write `db = await PsqlDB(...).connect()`. if called
again on an already-connected instance the previous pool is closed first (no
orphaned pool); if the SELECT-1 validation fails the freshly-built pool is torn
down before re-raising, so a failed connect() never leaks a live pool.
""" """
if self._pool is not None:
await self.close()
pool = await asyncpg.create_pool(**self._config)
try: try:
self._pool = await asyncpg.create_pool(**self._config) await pool.fetchval("SELECT 1")
await self._pool.fetchval("SELECT 1")
except _DRIVER_ERRORS: except _DRIVER_ERRORS:
log.exception("psql.connect() failed") log.exception("psql.connect() validation failed")
await pool.close()
raise raise
except BaseException:
await pool.close()
raise
self._pool = pool
return self return self
async def close(self) -> None: async def close(self) -> None:
@@ -338,8 +348,16 @@ class _Transaction:
async def __aenter__(self): async def __aenter__(self):
self._conn = await self._pool.acquire() self._conn = await self._pool.acquire()
try:
self._tx = self._conn.transaction() self._tx = self._conn.transaction()
await self._tx.start() await self._tx.start()
except BaseException:
# start() (or transaction()) failing after acquire would otherwise leak the
# pooled connection — __aexit__ is not called when __aenter__ raises. release
# it and reset so a failed transaction start never burns a pool slot.
await self._pool.release(self._conn)
self._conn = None
raise
return self._conn return self._conn
async def __aexit__(self, exc_type, exc, tb) -> None: async def __aexit__(self, exc_type, exc, tb) -> None: