embeddings: register pool close() at interpreter exit

Without this, every `second-brain process` run prints
  couldn't stop thread 'pool-1-worker-N' within 5.0 seconds
during interpreter shutdown — psycopg_pool's background workers don't
get a clean stop signal before Python's threading shutdown deadline.

Registering close_pool via atexit the first time we open the pool fixes
it without changing any API. `close_pool` is already idempotent, so the
explicit teardown path in the smoke test (which calls it directly) and
the atexit path coexist safely.
This commit is contained in:
Travis Herbranson 2026-05-24 23:33:13 -04:00
parent f44ac2ff7c
commit 0d9ebc9cc7

View File

@ -23,6 +23,7 @@ index, not authoritative state.
from __future__ import annotations from __future__ import annotations
import atexit
import logging import logging
import os import os
from typing import Any from typing import Any
@ -73,6 +74,11 @@ def _get_pool() -> ConnectionPool | None:
if not dsn: if not dsn:
return None return None
_pool = ConnectionPool(dsn, min_size=1, max_size=4, open=True, timeout=10) _pool = ConnectionPool(dsn, min_size=1, max_size=4, open=True, timeout=10)
# Make sure the pool's worker threads get a clean shutdown when the
# interpreter exits; without this psycopg_pool prints
# "couldn't stop thread pool-1-worker-N within 5.0 seconds" on every
# `second-brain process` run.
atexit.register(close_pool)
return _pool return _pool