fix: ensure DB connections are fully released on close#146
Merged
Conversation
TargetDB.close() was only calling session.close(), which returns connections to SQLAlchemy's pool but does not close the underlying TCP sockets. On abnormal process termination (exceptions, SIGINT, etc.) the engine is never garbage-collected, leaving idle connections on the PostgreSQL server. Add engine.dispose() to close() so that all pooled connections are released to the server when the caller is done. Also add __enter__ and __exit__ to support usage as a context manager. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 5 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
2 tasks
Open
2 tasks
There was a problem hiding this comment.
Pull request overview
This PR improves lifecycle management for TargetDB by fully releasing SQLAlchemy pooled connections when closing, and adds context manager support to encourage safer usage patterns.
Changes:
- Update
TargetDB.close()to callengine.dispose()aftersession.close()so pooled DB connections are fully released. - Add
TargetDB.__enter__/__exit__to enablewith TargetDB(...) as db:usage. - Regenerate/update
tbls-generated table documentation (index ordering changes only).
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/targetdb/targetdb.py | Disposes SQLAlchemy engine on close and adds context manager support. |
| docs/tbls/public.target.md | Updates generated index listing order/formatting. |
| docs/tbls/public.sky.md | Updates generated index listing order/formatting. |
| docs/tbls/public.fluxstd.md | Updates generated index listing order/formatting. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Wrap session.close() in try/finally so engine.dispose() always runs, even if session.close() itself raises an exception. - Make __enter__() idempotent: skip connect() if a session already exists, preventing an engine/session leak when called on an already-connected instance (e.g. db.connect(); with db:). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TargetDB.close()was callingsession.close()only, which returns connections to SQLAlchemy's internal pool but does not close the underlying TCP sockets to PostgreSQL.engine.dispose()toclose()to fully release all pooled connections to the server.__enter__/__exit__to support thewithstatement for safer usage.Root cause
Test plan
db.close(), the connection no longer appears inpg_stat_activityTargetDBcontinue to work normallywith TargetDB(...) as db: ...🤖 Generated with Claude Code