fix: close leaked sqlite connections in scheduled_tasks, message_history, conversations#16
Merged
Merged
Conversation
…ory, conversations with sqlite3.connect(...) as conn only commits/rolls back the transaction on exit, it never closes the connection or releases its file descriptor. ScheduledTasks.run() opens fresh connections every 30s forever, so over enough uptime these leak until the process hits its fd ulimit, causing "unable to open database file" everywhere (scheduled tasks, /list, and likely the Telegram poller's sockets too). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UfuBc1EqfevpENRBAmTMrA
Contributor
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses long-running file descriptor leaks caused by sqlite3 connections not being closed (notably in the scheduled task loop), which can eventually trigger “unable to open database file” and other cascading failures.
Changes:
- Adds explicit
conn.close()calls after sqlite operations inScheduledTasksandMessageHistory. - Refactors
ConversationStore’s_fk_conncontext manager to ensure connections are closed after use. - Adjusts query/insert flows to close connections after work is done.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| app/infra/message_history.py | Adds explicit connection closes around message history reads/writes. |
| app/infra/conversations.py | Refactors _fk_conn to explicitly close sqlite connections used by ConversationStore. |
| app/core/scheduled_tasks.py | Adds explicit connection closes in scheduled task DB operations to prevent FD leaks over uptime. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
59
to
+63
| "VALUES (?, ?, ?, ?, ?, ?)", | ||
| (self.channel, role, content, timestamp, est, conversation_id), | ||
| ) | ||
| conn.commit() | ||
| conn.close() |
Comment on lines
68
to
+72
| rows = conn.execute("""SELECT role, content FROM messages | ||
| WHERE channel = ? | ||
| ORDER BY id DESC LIMIT ?""", (self.channel, limit)).fetchall() | ||
| return [{"role": row[0], "content": row[1]} for row in reversed(rows)] | ||
| conn.close() | ||
| return [{"role": row[0], "content": row[1]} for row in reversed(rows)] |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Rikul
added a commit
that referenced
this pull request
Jun 25, 2026
…ory, conversations (#16) * fix: close leaked sqlite connections in scheduled_tasks, message_history, conversations with sqlite3.connect(...) as conn only commits/rolls back the transaction on exit, it never closes the connection or releases its file descriptor. ScheduledTasks.run() opens fresh connections every 30s forever, so over enough uptime these leak until the process hits its fd ulimit, causing "unable to open database file" everywhere (scheduled tasks, /list, and likely the Telegram poller's sockets too). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UfuBc1EqfevpENRBAmTMrA * Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.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.
with sqlite3.connect(...) as conn only commits/rolls back the
transaction on exit, it never closes the connection or releases its
file descriptor. ScheduledTasks.run() opens fresh connections every
30s forever, so over enough uptime these leak until the process hits
its fd ulimit, causing "unable to open database file" everywhere
(scheduled tasks, /list, and likely the Telegram poller's sockets too).
Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_01UfuBc1EqfevpENRBAmTMrA