From 4ad40c48e95324ba756e24fd68a4b8357fe73952 Mon Sep 17 00:00:00 2001 From: Marco Gallegos Date: Mon, 22 Dec 2025 15:08:04 -0600 Subject: [PATCH 1/2] fix: Add missing `sqlite3` import and improve database connection robustness by verifying and safely closing connections. --- Tasks.md | 16 ++++++++++++++++ bot/db.py | 26 +++++++++++++++++++++----- bot/modules/flow_engine.py | 1 + 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Tasks.md b/Tasks.md index 87e28b7..a20794e 100644 --- a/Tasks.md +++ b/Tasks.md @@ -100,6 +100,22 @@ This document tracks all pending tasks, improvements, and issues identified in t - **Description**: `identity.py:42` string comparison for ADMIN_ID could fail if numeric - **Action needed**: Fix type handling for user ID comparison +### [BUG-004] Missing sqlite3 import +- **Status**: DONE +- **Priority**: High +- **Description**: `flow_engine.py` missing `sqlite3` import causing NameError +- **Files affected**: `flow_engine.py` +- **Action needed**: Add `import sqlite3` +- **Due**: ASAP + +### [BUG-005] Telegram Conflict Error +- **Status**: DONE +- **Priority**: High +- **Description**: `telegram.error.Conflict` indicates multiple bot instances running +- **Files affected**: Runtime +- **Action needed**: Kill all orphan processes and restart +- **Due**: ASAP + ## **Documentation & Testing** 📚 ### [DOC-001] Documentation Consistency diff --git a/bot/db.py b/bot/db.py index 016c707..be55bdc 100644 --- a/bot/db.py +++ b/bot/db.py @@ -15,16 +15,32 @@ local = threading.local() def get_db_connection(): """Creates a connection to the SQLite database.""" - if not hasattr(local, "conn"): - local.conn = sqlite3.connect(DATABASE_FILE, check_same_thread=False) - local.conn.row_factory = sqlite3.Row + if hasattr(local, "conn"): + try: + # Check if connection is open + local.conn.execute("SELECT 1") + logger.debug("Reusing existing database connection") + return local.conn + except sqlite3.ProgrammingError: + logger.warning("Detected closed connection in thread-local storage. Recreating.") + del local.conn + + logger.debug("Creating new database connection") + local.conn = sqlite3.connect(DATABASE_FILE, check_same_thread=False) + local.conn.row_factory = sqlite3.Row return local.conn def close_db_connection(): """Closes the database connection.""" if hasattr(local, "conn"): - local.conn.close() - del local.conn + logger.debug("Closing database connection") + try: + local.conn.close() + except Exception as e: + logger.error(f"Error closing database connection: {e}") + finally: + if hasattr(local, "conn"): + del local.conn def setup_database(): """Sets up the database tables if they don't exist.""" diff --git a/bot/modules/flow_engine.py b/bot/modules/flow_engine.py index f8d0cf6..c0c3fb8 100644 --- a/bot/modules/flow_engine.py +++ b/bot/modules/flow_engine.py @@ -2,6 +2,7 @@ import json import logging import os +import sqlite3 from bot.db import get_db_connection from bot.modules.sales_rag import generate_sales_pitch from bot.modules.nfc_tag import generate_nfc_tag From 7eb3535ba9691eb040d1bc5cdf41ef1be1d6b2e8 Mon Sep 17 00:00:00 2001 From: Marco Gallegos Date: Mon, 22 Dec 2025 15:09:42 -0600 Subject: [PATCH 2/2] Update status of BUG-004 to TODO --- Tasks.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tasks.md b/Tasks.md index a20794e..d76786d 100644 --- a/Tasks.md +++ b/Tasks.md @@ -101,7 +101,7 @@ This document tracks all pending tasks, improvements, and issues identified in t - **Action needed**: Fix type handling for user ID comparison ### [BUG-004] Missing sqlite3 import -- **Status**: DONE +- **Status**: TODO - **Priority**: High - **Description**: `flow_engine.py` missing `sqlite3` import causing NameError - **Files affected**: `flow_engine.py` @@ -184,4 +184,4 @@ This document tracks all pending tasks, improvements, and issues identified in t - `IN_PROGRESS` - Currently being worked on - `IN_REVIEW` - Ready for review - `DONE` - Completed -- `BLOCKED` - Blocked by dependency \ No newline at end of file +- `BLOCKED` - Blocked by dependency