Skip to content

[Bug] Database connections leak when exceptions are raised #5

Description

@github-actions

Description

Throughout the backend routers and services, database connections are obtained with get_db_connection() and closed manually at the end of each function. If any exception is raised between acquiring the connection and the close calls, the connection is never returned, causing a connection leak. Over time this will exhaust the PostgreSQL connection limit.

Location

Examples across multiple files:

  • backend/routers/auth.pylogin(), signup()
  • backend/routers/cases.pycreate_case(), apply_to_case(), get_recommended_cases_for_lawyer()
  • backend/routers/lawyers.pyupsert_lawyer_profile(), get_watchlist()
  • backend/services/matching_service.pyrefresh_lawyer_responsiveness()

Recommendation

Wrap all database operations in a try/finally block to guarantee the connection is always closed:

conn = get_db_connection()
try:
    cur = conn.cursor()
    cur.execute(...)
    conn.commit()
finally:
    cur.close()
    conn.close()

Alternatively, implement get_db_connection() as a context manager, or use a connection pool that automatically recycles connections.

Severity

High

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions