fix(gateway): checkpoint WAL before close to fix SQLite FD leak (#37369)#38740
Open
ashishpatel26 wants to merge 1 commit into
Open
fix(gateway): checkpoint WAL before close to fix SQLite FD leak (#37369)#38740ashishpatel26 wants to merge 1 commit into
ashishpatel26 wants to merge 1 commit into
Conversation
NousResearch#37369) ResponseStore.close() ran PRAGMA wal_checkpoint(PASSIVE) before conn.close() to flush WAL pages so SQLite can release its WAL and SHM sidecar file descriptors on close. Without this checkpoint the WAL/SHM FDs accumulate across request lifecycles on platforms where SQLite keeps them open. Also adds context-manager support (__enter__/__exit__) and a double-close guard (self._conn = None before I/O) so callers can use and concurrent close() calls are safe.
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.
Summary
ResponseStore.close()now runsPRAGMA wal_checkpoint(PASSIVE)beforeconn.close()to flush WAL pages and release the WAL/SHM sidecar file descriptorsself._conn = Nonebefore any I/O prevents a secondclose()from re-entering the exception paths__enter__/__exit__so callers can usewith ResponseStore() as store:patternRoot cause (fixes #37369)
SQLite in WAL mode keeps separate WAL and SHM sidecar files open. Without an explicit checkpoint before
conn.close(), some platforms never fully release the FDs for those sidecars. Over the lifetime of a busy gateway process this causes steady FD accumulation until the process hits the OS limit and crashes withToo many open files.The PASSIVE checkpoint flushes without blocking writers; on a busy server the checkpoint may not fully complete, but the goal is to release this connection's own FDs.
Test plan
tests/gateway/test_api_server.py— verifies checkpoint is called onclose(), that double-close is a no-op, and that__exit__delegates toclose()🤖 Generated with Claude Code