Skip to content

Fix(vendor): close db session when vendor not found in get_vendor_details#258

Open
Jean-Regis-M wants to merge 1 commit intoGenAI-Security-Project:mainfrom
Jean-Regis-M:patch-20
Open

Fix(vendor): close db session when vendor not found in get_vendor_details#258
Jean-Regis-M wants to merge 1 commit intoGenAI-Security-Project:mainfrom
Jean-Regis-M:patch-20

Conversation

@Jean-Regis-M
Copy link

Summary

Fixes database session leak in get_vendor_details() when vendor ID is not found (#144)

Problem

When get_vendor_details() is called with an invalid vendor ID, it raises
ValueError("Vendor not found") but fails to close the database session.
This leaks connections, causing connection pool exhaustion under load.

Root Cause

The function uses manual session management (db = next(get_db())) without
exception safety. When the exception is raised, execution jumps to the
caller without calling db.close().

Solution

Wrap the database operations in a try/finally block to guarantee
db.close() is called on all execution paths, including error paths.

Impact

  • No breaking changes
  • Minimal diff (only 4 lines changed)
  • Improved correctness - resources are always cleaned up
  • Matches fix pattern from INV-GET-004

Testing

  • test_vnd_get_004_db_session_not_closed_on_exception now passes
  • All existing test_vnd_get_* tests continue to pass
  • Manual verification of session cleanup

Fixes #144

…ails

Root cause:
get_vendor_details() uses manual session management (next(get_db())) but
lacks exception safety. When ValueError is raised for non-existent vendor,
the session is never closed, causing connection leaks.

Solution:
Wrap database operations in try/finally block to ensure db.close() is
called on all execution paths, including error paths.

Impact:
- Zero behavioral changes on success path
- Sessions now properly closed on error path
- Prevents connection pool exhaustion under load
- Matches fix pattern from similar issue INV-GET-004

Signed-off-by: JEAN REGIS <240509606@firat.edu.tr>
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.

Bug_042_MUST_FIX : Test Case VND-GET-004 — get_vendor_details leaks database session when vendor is not found

1 participant