Skip to content

fix: close SQLite connections in delivery_ledger, async_delegation, and verification_evidence ledgers - #69785

Closed
JonthanaHanh wants to merge 1 commit into
NousResearch:mainfrom
JonthanaHanh:fix/sqlite-connection-leak
Closed

fix: close SQLite connections in delivery_ledger, async_delegation, and verification_evidence ledgers#69785
JonthanaHanh wants to merge 1 commit into
NousResearch:mainfrom
JonthanaHanh:fix/sqlite-connection-leak

Conversation

@JonthanaHanh

Copy link
Copy Markdown
Contributor

Summary

All three ledger modules (delivery_ledger.py, async_delegation.py, verification_evidence.py) create a new sqlite3.Connection via _connect() on every database call but rely on the context-manager protocol (with conn:) which only handles commit/rollback — it never calls conn.close(). Each call leaks one file descriptor; under sustained gateway or cron load the process hits the OS fd limit.

Root Cause

sqlite3.Connection.__enter__ returns self and __exit__ calls commit() or rollback() but does not close the connection. Every with _connect() as conn: block leaks a connection until GC runs (which is non-deterministic).

Fix

Wrap every _connect() call with contextlib.closing() so the connection is explicitly closed on scope exit:

# Before
with _DB_LOCK, _connect() as conn:
    conn.execute(...)

# After
with _DB_LOCK, closing(_connect()) as conn:
    conn.execute(...)

Files Changed

  • gateway/delivery_ledger.py — 5 call sites
  • tools/async_delegation.py — 13 call sites
  • agent/verification_evidence.py — 3 call sites

Fixes

Fixes #69678
Fixes #69567

…nd verification_evidence ledgers

All three ledger modules create a new sqlite3.Connection via _connect()
on every call but rely on the context-manager protocol (with conn:) which
only handles commit/rollback — it never calls conn.close(). Each call
leaks one file descriptor; under sustained gateway or cron load the
process hits the OS fd limit.

Fix: wrap every _connect() call with contextlib.closing() so the
connection is closed on scope exit, matching the pattern already used
elsewhere in the codebase.

Fixes NousResearch#69678
Fixes NousResearch#69567
@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/gateway Gateway runner, session dispatch, delivery tool/delegate Subagent delegation sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages duplicate This issue or pull request already exists labels Jul 23, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

Duplicate of #69681: both close the same SQLite connections in delivery_ledger, async_delegation, and verification_evidence. #69681 is the earlier open patch and additionally covers schema-init and failure paths with regression tests.

@teknium1

Copy link
Copy Markdown
Contributor

Closing: same fix as #69681 (which was submitted earlier and included tests) — the bug class is resolved on main via #70985, which credits both base PRs. Thanks for the minimal, correct patch.

@teknium1 teknium1 closed this Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/gateway Gateway runner, session dispatch, delivery duplicate This issue or pull request already exists P1 High — major feature broken, no workaround sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state tool/delegate Subagent delegation type/bug Something isn't working

Projects

None yet

3 participants