fix: ResponseStore SQLite connection pooling to prevent FD leak (#36111, #37369)#37389
Open
datin-antasena wants to merge 1 commit into
Open
fix: ResponseStore SQLite connection pooling to prevent FD leak (#36111, #37369)#37389datin-antasena wants to merge 1 commit into
datin-antasena wants to merge 1 commit into
Conversation
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
Fixes the file descriptor leak in
ResponseStorethat causes the gateway to hitOSError: [Errno 24] Too many open filesafter ~2 days of uptime.Credit: This issue was first identified by @jscoltock in #36111 (May 31), with excellent root cause analysis. We independently hit the same problem in production (#37369) and built on their findings to implement the fix. Their proposed approach (singleton + close()) was the right direction.
Root Cause
ResponseStore.__init__()opens a new SQLite connection every time anAPIServerAdapteris created (line 706), but:close()method was called on adapter teardownAfter ~45 hours with 8 Telegram topics, we observed 15 FDs to
response_store.db(7-8 separate connections) when 3 is the healthy baseline.Fix
Connection pooling with reference counting:
_connection_pool(class-level dict) keyed by resolved DB path_ResponseStoreConnectionwrapssqlite3.Connectionwith anRLockandref_countResponseStore()instances for the same file share ONE underlying connectionclose()decrements ref_count; connection is only closed when last reference is released__del__as safety netAPIServerAdapter.disconnect()now callsstore.close()APIServerAdapter.connect()recreates store if adapter is reused after disconnectThread safety: All store operations (
get,put,delete,set_conversation,get_conversation) are serialized viashared.lock(RLock).Tests
test_reuses_one_connection_per_db_path— verifies connection poolingtest_adapter_disconnect_closes_response_store— verifies adapter lifecycle cleanupVerification
Closes #37369
Closes #36111