fix: log-and-reraise create_pool failures; lock close() against a racing connect()

connect() built the pool with `await asyncpg.create_pool(...)` outside the
try that wraps SELECT-1 validation - with the default min_size=1 the pool
connects eagerly, so a bad host/auth propagated with no log.exception line,
breaking the documented "every method logs then re-raises" contract.
create_pool() now sits inside the same try/log/re-raise as validation, and
either failure point tears down a partially-built pool before re-raising.

close() didn't take _connect_lock while connect() did, so a close() racing
an in-flight connect() would see _pool is None and no-op as success while
connect() went on to install a live pool - a shutdown handler racing a
reconnect could "close" the instance while real connections stayed open.
close() now takes the same lock via a shared _close_locked() helper.

Signed-off-by: disqualifier <dev@disqualifier.me>
This commit is contained in:
2026-07-03 19:11:52 -04:00
parent d619d4ca55
commit e4c61cf6c3
3 changed files with 23 additions and 10 deletions
+3 -3
View File
@@ -10,18 +10,18 @@ a sibling of the `mongo` lib. Class is **`PsqlDB`**.
`requirements.txt`:
```
psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v0.1.6
psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v0.1.7
```
Direct:
```bash
pip install "psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v0.1.6"
pip install "psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v0.1.7"
```
Pulls `asyncpg`.
Drop the `@v0.1.6` suffix from the line above to install the latest unpinned.
Drop the `@v0.1.7` suffix from the line above to install the latest unpinned.
## The two-layer API
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "psql"
version = "0.1.6"
version = "0.1.7"
description = "async postgres wrapper over asyncpg: two-layer API (friendly verbs + raw escape hatch), fail-loud, config-free"
requires-python = ">=3.10"
dependencies = [
+19 -6
View File
@@ -123,23 +123,36 @@ class PsqlDB:
"""
async with self._connect_lock:
if self._pool is not None:
await self.close()
pool = await asyncpg.create_pool(**self._config)
await self._close_locked()
pool = None
try:
pool = await asyncpg.create_pool(**self._config)
await pool.fetchval("SELECT 1")
except _DRIVER_ERRORS:
log.exception("psql.connect() validation failed")
await pool.close()
log.exception("psql.connect() failed")
if pool is not None:
await pool.close()
raise
except BaseException:
await pool.close()
if pool is not None:
await pool.close()
raise
self._pool = pool
return self
async def close(self) -> None:
"""close the pool on shutdown and null the reference (so pool/connected checks
report not-connected against a dead pool)"""
report not-connected against a dead pool)
guarded by the same lock as connect() - a close() racing an in-flight connect()
waits for it rather than no-opping against a not-yet-installed pool and leaving
the just-built one live.
"""
async with self._connect_lock:
await self._close_locked()
async def _close_locked(self) -> None:
"""close the pool and null the reference; caller must hold `_connect_lock`"""
if self._pool is None:
return
try: