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:
@@ -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.6
|
psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v0.1.7
|
||||||
```
|
```
|
||||||
|
|
||||||
Direct:
|
Direct:
|
||||||
|
|
||||||
```bash
|
```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`.
|
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
|
## The two-layer API
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "psql"
|
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"
|
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 = [
|
||||||
|
|||||||
+19
-6
@@ -123,23 +123,36 @@ class PsqlDB:
|
|||||||
"""
|
"""
|
||||||
async with self._connect_lock:
|
async with self._connect_lock:
|
||||||
if self._pool is not None:
|
if self._pool is not None:
|
||||||
await self.close()
|
await self._close_locked()
|
||||||
pool = await asyncpg.create_pool(**self._config)
|
pool = None
|
||||||
try:
|
try:
|
||||||
|
pool = await asyncpg.create_pool(**self._config)
|
||||||
await pool.fetchval("SELECT 1")
|
await pool.fetchval("SELECT 1")
|
||||||
except _DRIVER_ERRORS:
|
except _DRIVER_ERRORS:
|
||||||
log.exception("psql.connect() validation failed")
|
log.exception("psql.connect() failed")
|
||||||
await pool.close()
|
if pool is not None:
|
||||||
|
await pool.close()
|
||||||
raise
|
raise
|
||||||
except BaseException:
|
except BaseException:
|
||||||
await pool.close()
|
if pool is not None:
|
||||||
|
await pool.close()
|
||||||
raise
|
raise
|
||||||
self._pool = pool
|
self._pool = pool
|
||||||
return self
|
return self
|
||||||
|
|
||||||
async def close(self) -> None:
|
async def close(self) -> None:
|
||||||
"""close the pool on shutdown and null the reference (so pool/connected checks
|
"""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:
|
if self._pool is None:
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user