Skip to content

Close the pgvector connection pool before interpreter shutdown - #162

Merged
rajivpant merged 1 commit into
mainfrom
fix/pool-close-at-exit
Nov 6, 2026
Merged

rajivpant merged 1 commit into
mainfrom
fix/pool-close-at-exit

Conversation

@rajivpant

Copy link
Copy Markdown
Collaborator

Every CLI command that touched the database (ragbot db status, ragbot index, ...) ended with this on Python 3.14:

Exception ignored while calling deallocator <function ConnectionPool.__del__ ...>:
  ...
PythonFinalizationError: cannot join thread at interpreter shutdown

The cached backend kept its psycopg_pool.ConnectionPool alive until interpreter finalization. The pool's __del__ then tried to join its worker threads, which Python 3.14 refuses at that point. Ragbot moved to 3.14 in #158.

Change

  • PgvectorBackend._ensure_pool registers an atexit hook that closes the pool. Exit hooks run before finalization, while threads can still be joined. The hook holds a weak reference, so it never keeps a discarded backend's pool, and its connections, open.
  • PgvectorBackend.close() closes the pool and is idempotent. VectorStore.close() is a no-op default for backends that hold no connections.
  • reset_vector_store() closes the cached backend before dropping it, so tests that reset no longer leave pools open.

Verification

  • ragbot db status against a live database: the traceback appears on main and not with this change.
  • New unit tests cover the exit hook registration and its weak reference, the hook on a collected or already-closed pool, close() idempotence, and reset_vector_store() closing the backend.
  • A new live test runs a real interpreter exit in a subprocess. The backend stays referenced until shutdown, as get_vector_store()'s cache keeps it in the CLI, and the test asserts stderr is clean. It fails on main with PythonFinalizationError and passes here.
  • make test-fast is green. With a throwaway pgvector 18 database (app and migration roles split, as in compose), the full suite passes, including the live tests.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rajivpant
rajivpant merged commit 1739c9a into main Nov 6, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant