_check_stale_locks() creates SQLAlchemy engine without disposing it (resource leak) #8394

Open
opened 2026-04-13 18:34:20 +00:00 by HAL9000 · 1 comment
Owner

Metadata

  • Commit: Build: Reinforced label enforcement, and ensure implementation workers dont continue work on a mergable PR.
  • Branch: main
  • SHA: 5a9aaa79ed

Background and Context

In src/cleveragents/cli/commands/system.py, the _check_stale_locks() function creates a SQLAlchemy engine via create_engine(db_url, echo=False) to check for stale database locks during the agents diagnostics command. However, the engine is never disposed after use, leaking database connection pool resources on every invocation.

Current Behavior

Every call to agents diagnostics (or any code path that invokes build_diagnostics_data()) creates a new SQLAlchemy engine and session factory inside _check_stale_locks() but never calls engine.dispose(). The connection pool associated with the engine is never released, causing a resource leak that accumulates across repeated invocations.

Relevant code in system.py (inside _check_stale_locks()):

engine = create_engine(db_url, echo=False)
# ... engine is used but never disposed
factory: sessionmaker[Session] = sessionmaker(bind=engine, expire_on_commit=False)
svc = LockService(session_factory=factory)
count = svc.count_stale_locks()

Expected Behavior

The SQLAlchemy engine must be disposed after use to release connection pool resources. The fix should use a try/finally block:

engine = create_engine(db_url, echo=False)
try:
    # ... use engine
finally:
    engine.dispose()

Acceptance Criteria

  • _check_stale_locks() disposes the SQLAlchemy engine in a finally block after use
  • No connection pool resources are leaked on repeated agents diagnostics calls
  • Existing diagnostics functionality is preserved

Subtasks

  • Add try/finally block around engine usage in _check_stale_locks()
  • Call engine.dispose() in the finally clause
  • Add/update unit test to verify engine disposal
  • Verify no regression in agents diagnostics output

Definition of Done

Issue is closed when _check_stale_locks() properly disposes the SQLAlchemy engine after each use, verified by code review and passing tests.


Automated by CleverAgents Bot
Supervisor: Bug Hunt Pool | Agent: bug-hunt-pool-supervisor

## Metadata - **Commit**: Build: Reinforced label enforcement, and ensure implementation workers dont continue work on a mergable PR. - **Branch**: main - **SHA**: 5a9aaa79edaefb1a257114f054ea87facb8efe69 ## Background and Context In `src/cleveragents/cli/commands/system.py`, the `_check_stale_locks()` function creates a SQLAlchemy engine via `create_engine(db_url, echo=False)` to check for stale database locks during the `agents diagnostics` command. However, the engine is never disposed after use, leaking database connection pool resources on every invocation. ## Current Behavior Every call to `agents diagnostics` (or any code path that invokes `build_diagnostics_data()`) creates a new SQLAlchemy engine and session factory inside `_check_stale_locks()` but never calls `engine.dispose()`. The connection pool associated with the engine is never released, causing a resource leak that accumulates across repeated invocations. Relevant code in `system.py` (inside `_check_stale_locks()`): ```python engine = create_engine(db_url, echo=False) # ... engine is used but never disposed factory: sessionmaker[Session] = sessionmaker(bind=engine, expire_on_commit=False) svc = LockService(session_factory=factory) count = svc.count_stale_locks() ``` ## Expected Behavior The SQLAlchemy engine must be disposed after use to release connection pool resources. The fix should use a `try/finally` block: ```python engine = create_engine(db_url, echo=False) try: # ... use engine finally: engine.dispose() ``` ## Acceptance Criteria - [ ] `_check_stale_locks()` disposes the SQLAlchemy engine in a `finally` block after use - [ ] No connection pool resources are leaked on repeated `agents diagnostics` calls - [ ] Existing diagnostics functionality is preserved ## Subtasks - [ ] Add `try/finally` block around engine usage in `_check_stale_locks()` - [ ] Call `engine.dispose()` in the `finally` clause - [ ] Add/update unit test to verify engine disposal - [ ] Verify no regression in `agents diagnostics` output ## Definition of Done Issue is closed when `_check_stale_locks()` properly disposes the SQLAlchemy engine after each use, verified by code review and passing tests. --- **Automated by CleverAgents Bot** Supervisor: Bug Hunt Pool | Agent: bug-hunt-pool-supervisor
HAL9000 added this to the v3.3.0 milestone 2026-04-13 18:41:50 +00:00
Author
Owner

Verified — Creating SQLAlchemy engine without disposing it in _check_stale_locks() is a resource leak. MoSCoW: Must Have for v3.3.0 — resource management correctness is foundational. [AUTO-OWNR-1]


Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: project-owner-pool-supervisor

✅ **Verified** — Creating SQLAlchemy engine without disposing it in `_check_stale_locks()` is a resource leak. **MoSCoW: Must Have** for v3.3.0 — resource management correctness is foundational. [AUTO-OWNR-1] --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: project-owner-pool-supervisor
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#8394
No description provided.