Skip to content

fix: close SQLite connections in 3 ledgers (delivery_ledger, async_delegation, verification_evidence) - #70918

Closed
kshitijk4poor wants to merge 1 commit into
NousResearch:mainfrom
kshitijk4poor:salvage-69678-sqlite-leak
Closed

fix: close SQLite connections in 3 ledgers (delivery_ledger, async_delegation, verification_evidence)#70918
kshitijk4poor wants to merge 1 commit into
NousResearch:mainfrom
kshitijk4poor:salvage-69678-sqlite-leak

Conversation

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Summary

Fixes SQLite connection FD leaks in 3 ledgers that exhaust RLIMIT_NOFILE in long-running gateway processes.

Changes

  • gateway/delivery_ledger.py: Add _transaction() context manager that guarantees conn.close() — replaces 5 with _connect() as conn: sites
  • tools/async_delegation.py: Same fix — replaces 13 leak sites
  • agent/verification_evidence.py: Same fix — replaces 3 leak sites
  • 6 regression tests (2 per module) verifying connection is closed on both normal exit and exception

Root cause

sqlite3.Connection.__exit__ only commits/rollbacks; it does NOT close the FD. Every operation opened a connection that was never closed, accumulating until [Errno 24] Too many open files.

Fixes #69678.

Salvaged from @webtecnica's PR #70530 (commit 3 of 3).

Validation

Before After
delivery_ledger tests 31 passed 33 passed (+2 new)
verification_evidence tests 19 passed 21 passed (+2 new)
async_delegation new tests 2 passed
E2E (real imports) FD leak conn closed after _transaction exit

…erification_evidence (NousResearch#69678)

Add _transaction() context manager that guarantees conn.close() on exit,
matching the accepted fix pattern from NousResearch#69567 (kanban_db.connect_closing).

Root cause: sqlite3.Connection.__exit__ only commits/rollbacks; it does NOT
close the file descriptor. In long-lived processes every _connect() call
leaked an FD, accumulating until RLIMIT_NOFILE exhaustion.

Changes:
- Add _transaction() context manager to all 3 modules with guaranteed
  conn.close() on exit (commit on success, rollback on exception).
- Replace every with _connect() as conn: with with _transaction() as conn:
  (5 sites in delivery_ledger.py, 13 in async_delegation.py,
   3 in verification_evidence.py).
- Add 2 connection-closing regression tests per test suite (normal exit +
  exception path).

Fixes NousResearch#69678
@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 duplicate This issue or pull request already exists labels Jul 24, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

Duplicate of #69681: this focused salvage uses the same deterministic close mechanism for the delivery, async-delegation, and verification-evidence ledgers. #69681 is the earlier open canonical and additionally covers schema-init and failure-path closure.

@teknium1

Copy link
Copy Markdown
Contributor

Closing: duplicate of #69681 — fixed on main via #70985 with the earliest submitter's authorship.

@teknium1 teknium1 closed this Jul 24, 2026
@kshitijk4poor
kshitijk4poor deleted the salvage-69678-sqlite-leak branch August 5, 2026 07:09
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 tool/delegate Subagent delegation type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQLite connections leaked (no close) in delivery_ledger, async_delegation, and verification_evidence ledgers — fd exhaustion, same class as #69567

4 participants