fix(gateway,tools,agent): close leaked SQLite connections in delivery… - #69681
Closed
dhruvraajeev wants to merge 1 commit into
Closed
fix(gateway,tools,agent): close leaked SQLite connections in delivery…#69681dhruvraajeev wants to merge 1 commit into
dhruvraajeev wants to merge 1 commit into
Conversation
…, delegation, and verification ledgers Three durable ledgers used `with _connect() as conn:` where the sqlite3 connection context manager commits/rolls back but never closes, leaking the db/-wal/-shm file descriptors on every call. On a long-running gateway this exhausts RLIMIT_NOFILE and fails unrelated components with `[Errno 24] Too many open files`. Same bug class as the cron execution ledger (NousResearch#69567 / PR NousResearch#69594), which the connection helpers here are modeled on. Fix: route every ledger operation through a `_transaction()` context manager that guarantees `conn.close()` on exit. `_connect()` keeps its schema-on-connect contract (several tests call it directly) and now self-closes if schema init fails. Adds per-module regression tests asserting every opened connection is closed, including the no-op-update and exception-mid-transaction paths. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
|
looks mergeable The close-on-exit invariant is implemented consistently across all three SQLite ledgers, including no-op returns and failures during both statements and schema initialization. I reviewed the submitted head and a run-owned local semantic replay against current GitHub Security evidence:
Signed: GPT-5.6-sol-xhigh in Codex |
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes a file-descriptor leak in three durable SQLite ledgers. Each used
with _connect() as conn:, where thesqlite3.Connectioncontext managercommits/rolls back but never closes the connection — so every operation leaked
the connection and its
-wal/-shmdescriptors, deferring the close to GC. Ona long-running gateway this drifts toward
RLIMIT_NOFILEand fails unrelatedcomponents with
[Errno 24] Too many open files.This is the same bug class as the cron execution ledger (#69567), fixed in
PR #69594; these three ledgers share the identical
_connect()pattern andwere not covered by that fix.
gateway/delivery_ledger.pyruns on everyoutbound final response, so it is the highest-frequency leaker.
Related Issue
Fixes #69678
Type of Change
Changes Made
gateway/delivery_ledger.py— added a_transaction()context manager thatalways
close()s; routed all 5 sites through it. The lock-free_prune()site stays lock-free.
tools/async_delegation.py— same_transaction()wrapper; routed all 13durable-ledger sites through it.
agent/verification_evidence.py— same_transaction()wrapper; routed all3 sites through it.
_connect()keeps its schema-on-connect contract (severaltests call it directly) and now self-closes if schema init fails.
tests/gateway/test_delivery_ledger_fd_leak.py,tests/tools/test_async_delegation_fd_leak.py,tests/agent/test_verification_evidence_fd_leak.py.Locking is unchanged:
_transaction()never acquires the module lock, so eachcall site keeps its exact existing
_DB_LOCKbehavior (no new lock nesting,no deadlock risk).
How to Test
pytest tests/gateway/test_delivery_ledger_fd_leak.py tests/tools/test_async_delegation_fd_leak.py tests/agent/test_verification_evidence_fd_leak.py -q→ all pass. Each wraps
sqlite3.connectto count opens vs closes andasserts every opened connection is closed (happy path, no-op update,
exception mid-transaction, and schema-init failure).
close()makes the happy-path/early-return/exception testsfail with
len(closed) == 0, confirming they catch the regression.pytest tests/gateway/test_delivery_ledger.py tests/gateway/test_delivery_ledger_producer.py tests/tools/test_async_delegation.py tests/agent/test_verification_evidence.py -qChecklist
fix(...))