From e9a575d3c480dc582f2aa277cd40aa5ed272795b Mon Sep 17 00:00:00 2001 From: stealthwhizz Date: Thu, 19 Mar 2026 00:15:35 +0530 Subject: [PATCH 1/2] merge: sync fork with upstream main --- finbot/tools/data/vendor.py | 117 +++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 56 deletions(-) diff --git a/finbot/tools/data/vendor.py b/finbot/tools/data/vendor.py index 7cac2cd3..95566c43 100644 --- a/finbot/tools/data/vendor.py +++ b/finbot/tools/data/vendor.py @@ -4,7 +4,7 @@ from typing import Any from finbot.core.auth.session import SessionContext -from finbot.core.data.database import db_session +from finbot.core.data.database import get_db from finbot.core.data.repositories import VendorRepository logger = logging.getLogger(__name__) @@ -23,12 +23,15 @@ async def get_vendor_details( Dictionary containing vendor details """ logger.info("Getting vendor details for vendor_id: %s", vendor_id) - with db_session() as db: + db = next(get_db()) + try: vendor_repo = VendorRepository(db, session_context) vendor = vendor_repo.get_vendor(vendor_id) if not vendor: raise ValueError("Vendor not found") return vendor.to_dict() + finally: + db.close() async def get_vendor_contact_info( @@ -37,20 +40,20 @@ async def get_vendor_contact_info( ) -> dict[str, Any]: """Get vendor contact information for communication purposes""" logger.info("Getting vendor contact info for vendor_id: %s", vendor_id) - with db_session() as db: - vendor_repo = VendorRepository(db, session_context) - vendor = vendor_repo.get_vendor(vendor_id) - if not vendor: - raise ValueError("Vendor not found") - - return { - "vendor_id": vendor.id, - "company_name": vendor.company_name, - "contact_name": vendor.contact_name, - "email": vendor.email, - "phone": vendor.phone, - "status": vendor.status, - } + db = next(get_db()) + vendor_repo = VendorRepository(db, session_context) + vendor = vendor_repo.get_vendor(vendor_id) + if not vendor: + raise ValueError("Vendor not found") + + return { + "vendor_id": vendor.id, + "company_name": vendor.company_name, + "contact_name": vendor.contact_name, + "email": vendor.email, + "phone": vendor.phone, + "status": vendor.status, + } async def update_vendor_status( @@ -70,32 +73,34 @@ async def update_vendor_status( risk_level, agent_notes, ) - with db_session() as db: - vendor_repo = VendorRepository(db, session_context) - vendor = vendor_repo.get_vendor(vendor_id) - if not vendor: - raise ValueError("Vendor not found") - - previous_state = { - "status": vendor.status, - "trust_level": vendor.trust_level, - "risk_level": vendor.risk_level, - } - - existing_notes = vendor.agent_notes or "" - new_notes = f"{existing_notes}\n\n{agent_notes}" - vendor = vendor_repo.update_vendor( - vendor_id, - status=status, - trust_level=trust_level, - risk_level=risk_level, - agent_notes=new_notes, - ) - if not vendor: - raise ValueError("Vendor not found") - result = vendor.to_dict() - result["_previous_state"] = previous_state - return result + db = next(get_db()) + vendor_repo = VendorRepository(db, session_context) + # append notes to the existing agent_notes + vendor = vendor_repo.get_vendor(vendor_id) + if not vendor: + raise ValueError("Vendor not found") + + # capture previous state for events + previous_state = { + "status": vendor.status, + "trust_level": vendor.trust_level, + "risk_level": vendor.risk_level, + } + + existing_notes = vendor.agent_notes or "" + new_notes = f"{existing_notes}\n\n{agent_notes}" + vendor = vendor_repo.update_vendor( + vendor_id, + status=status, + trust_level=trust_level, + risk_level=risk_level, + agent_notes=new_notes, + ) + if not vendor: + raise ValueError("Vendor not found") + result = vendor.to_dict() + result["_previous_state"] = previous_state + return result async def update_vendor_agent_notes( @@ -109,17 +114,17 @@ async def update_vendor_agent_notes( vendor_id, agent_notes, ) - with db_session() as db: - vendor_repo = VendorRepository(db, session_context) - vendor = vendor_repo.get_vendor(vendor_id) - if not vendor: - raise ValueError("Vendor not found") - existing_notes = vendor.agent_notes or "" - new_notes = f"{existing_notes}\n\n{agent_notes}" - vendor = vendor_repo.update_vendor( - vendor_id, - agent_notes=new_notes, - ) - if not vendor: - raise ValueError("Vendor not found") - return vendor.to_dict() + db = next(get_db()) + vendor_repo = VendorRepository(db, session_context) + vendor = vendor_repo.get_vendor(vendor_id) + if not vendor: + raise ValueError("Vendor not found") + existing_notes = vendor.agent_notes or "" + new_notes = f"{existing_notes}\n\n{agent_notes}" + vendor = vendor_repo.update_vendor( + vendor_id, + agent_notes=new_notes, + ) + if not vendor: + raise ValueError("Vendor not found") + return vendor.to_dict() From 8419c28a2a8b31a13e021dc575f5239d5179679f Mon Sep 17 00:00:00 2001 From: stealthwhizz Date: Fri, 5 Jun 2026 02:40:44 +0530 Subject: [PATCH 2/2] fix(vendor): close db sessions using db_session context manager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace next(get_db()) with db_session() context manager in all vendor tool functions that were leaking database connections. get_db() is a FastAPI generator meant only for Depends() injection. Calling next() on it directly bypasses the finally block that closes the session. db_session() is the correct pattern for non-FastAPI call sites — it closes the session on both success and exception paths. Affected functions: - get_vendor_contact_info: session never closed on any path - update_vendor_status: session never closed on any path - update_vendor_agent_notes: session never closed on any path get_vendor_details already had try/finally: db.close() from PR #258 — replaced with db_session() for consistency. Closes #144 Closes #148 Co-Authored-By: JEAN REGIS <240509606@firat.edu.tr> --- finbot/tools/data/vendor.py | 118 +++++++++++++++++------------------- 1 file changed, 57 insertions(+), 61 deletions(-) diff --git a/finbot/tools/data/vendor.py b/finbot/tools/data/vendor.py index 95566c43..9cec73e5 100644 --- a/finbot/tools/data/vendor.py +++ b/finbot/tools/data/vendor.py @@ -4,7 +4,7 @@ from typing import Any from finbot.core.auth.session import SessionContext -from finbot.core.data.database import get_db +from finbot.core.data.database import db_session from finbot.core.data.repositories import VendorRepository logger = logging.getLogger(__name__) @@ -23,15 +23,12 @@ async def get_vendor_details( Dictionary containing vendor details """ logger.info("Getting vendor details for vendor_id: %s", vendor_id) - db = next(get_db()) - try: + with db_session() as db: vendor_repo = VendorRepository(db, session_context) vendor = vendor_repo.get_vendor(vendor_id) if not vendor: raise ValueError("Vendor not found") return vendor.to_dict() - finally: - db.close() async def get_vendor_contact_info( @@ -40,20 +37,20 @@ async def get_vendor_contact_info( ) -> dict[str, Any]: """Get vendor contact information for communication purposes""" logger.info("Getting vendor contact info for vendor_id: %s", vendor_id) - db = next(get_db()) - vendor_repo = VendorRepository(db, session_context) - vendor = vendor_repo.get_vendor(vendor_id) - if not vendor: - raise ValueError("Vendor not found") - - return { - "vendor_id": vendor.id, - "company_name": vendor.company_name, - "contact_name": vendor.contact_name, - "email": vendor.email, - "phone": vendor.phone, - "status": vendor.status, - } + with db_session() as db: + vendor_repo = VendorRepository(db, session_context) + vendor = vendor_repo.get_vendor(vendor_id) + if not vendor: + raise ValueError("Vendor not found") + + return { + "vendor_id": vendor.id, + "company_name": vendor.company_name, + "contact_name": vendor.contact_name, + "email": vendor.email, + "phone": vendor.phone, + "status": vendor.status, + } async def update_vendor_status( @@ -73,34 +70,33 @@ async def update_vendor_status( risk_level, agent_notes, ) - db = next(get_db()) - vendor_repo = VendorRepository(db, session_context) - # append notes to the existing agent_notes - vendor = vendor_repo.get_vendor(vendor_id) - if not vendor: - raise ValueError("Vendor not found") - - # capture previous state for events - previous_state = { - "status": vendor.status, - "trust_level": vendor.trust_level, - "risk_level": vendor.risk_level, - } - - existing_notes = vendor.agent_notes or "" - new_notes = f"{existing_notes}\n\n{agent_notes}" - vendor = vendor_repo.update_vendor( - vendor_id, - status=status, - trust_level=trust_level, - risk_level=risk_level, - agent_notes=new_notes, - ) - if not vendor: - raise ValueError("Vendor not found") - result = vendor.to_dict() - result["_previous_state"] = previous_state - return result + with db_session() as db: + vendor_repo = VendorRepository(db, session_context) + vendor = vendor_repo.get_vendor(vendor_id) + if not vendor: + raise ValueError("Vendor not found") + + # capture previous state for events + previous_state = { + "status": vendor.status, + "trust_level": vendor.trust_level, + "risk_level": vendor.risk_level, + } + + existing_notes = vendor.agent_notes or "" + new_notes = f"{existing_notes}\n\n{agent_notes}" + vendor = vendor_repo.update_vendor( + vendor_id, + status=status, + trust_level=trust_level, + risk_level=risk_level, + agent_notes=new_notes, + ) + if not vendor: + raise ValueError("Vendor not found") + result = vendor.to_dict() + result["_previous_state"] = previous_state + return result async def update_vendor_agent_notes( @@ -114,17 +110,17 @@ async def update_vendor_agent_notes( vendor_id, agent_notes, ) - db = next(get_db()) - vendor_repo = VendorRepository(db, session_context) - vendor = vendor_repo.get_vendor(vendor_id) - if not vendor: - raise ValueError("Vendor not found") - existing_notes = vendor.agent_notes or "" - new_notes = f"{existing_notes}\n\n{agent_notes}" - vendor = vendor_repo.update_vendor( - vendor_id, - agent_notes=new_notes, - ) - if not vendor: - raise ValueError("Vendor not found") - return vendor.to_dict() + with db_session() as db: + vendor_repo = VendorRepository(db, session_context) + vendor = vendor_repo.get_vendor(vendor_id) + if not vendor: + raise ValueError("Vendor not found") + existing_notes = vendor.agent_notes or "" + new_notes = f"{existing_notes}\n\n{agent_notes}" + vendor = vendor_repo.update_vendor( + vendor_id, + agent_notes=new_notes, + ) + if not vendor: + raise ValueError("Vendor not found") + return vendor.to_dict()