Skip to content

fix: close leaked sqlite connections in scheduled_tasks, message_history, conversations#16

Merged
Rikul merged 2 commits into
mainfrom
claude/inspiring-bohr-1avfcd
Jun 25, 2026
Merged

fix: close leaked sqlite connections in scheduled_tasks, message_history, conversations#16
Rikul merged 2 commits into
mainfrom
claude/inspiring-bohr-1avfcd

Conversation

@Rikul

@Rikul Rikul commented Jun 24, 2026

Copy link
Copy Markdown
Owner

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

…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
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in ScheduledTasks and MessageHistory.
  • Refactors ConversationStore’s _fk_conn context 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 thread app/infra/conversations.py
Comment thread app/core/scheduled_tasks.py Outdated
Comment thread app/core/scheduled_tasks.py
Comment thread app/core/scheduled_tasks.py
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 Rikul merged commit df3cffb into main Jun 25, 2026
2 checks passed
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>
@Rikul Rikul deleted the claude/inspiring-bohr-1avfcd branch June 25, 2026 19:07
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.

3 participants