fix(gateway): release orphaned Telegram Application on failed connect (#14210) - #46313
Closed
tamio0800 wants to merge 1 commit into
Closed
fix(gateway): release orphaned Telegram Application on failed connect (#14210)#46313tamio0800 wants to merge 1 commit into
tamio0800 wants to merge 1 commit into
Conversation
Contributor
✅ Code Review: CleanReviewed the diff (
No issues found. The fix prevents CLOSE_WAIT socket accumulation that would eventually exhaust the fd limit on repeated gateway supervisor retries. |
Contributor
|
Automated hermes-sweeper review: this cleanup is already covered on current Evidence:
The prior review comment here was useful; the final mainline fix moved the cleanup to the gateway ownership boundary rather than duplicating it inside the old Telegram adapter path. |
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?
TelegramAdapter.connect()builds a PTBApplication(holding two httpx pools), then if a later step fails, the except handler returnsFalsewithout tearing the Application down. The supervisor retries with a fresh adapter and GCs the old one — but GC never closes httpx pools, so their sockets leak inCLOSE_WAITuntil the process dies. After days of reconnect retries this exhausts the fd limit (EMFILE).Fix: run the same teardown
disconnect()already does, guarded so a cleanup error can't mask the original failure.Complementary to #39336 (which raises the launchd fd cap and fixes steady-state pool sizing) — this stops the failure-path leak that accumulates regardless of the cap.
Related Issue
Refs #14210 (and its duplicate #14209)
Type of Change
Changes Made
gateway/platforms/telegram.py—connect()except handler now tears downself._app(stop updater → stop app →shutdown()) before releasing the lock, wrapped in try/except;_app/_botreset infinally. Original error handling unchanged.tests/gateway/test_telegram_connect_cleanup.py— 3 tests: cleanup runs on failure; safely skipped when_appnever built; cleanup error doesn't mask the original exception.How to Test
pytest tests/gateway/test_telegram_connect_cleanup.py -q→ 3 passedpytest tests/gateway/test_telegram_network_reconnect.py tests/gateway/test_telegram_network.py -q→ 61 passed (no regression)connect()to fail mid-startup; confirm TelegramCLOSE_WAITsockets no longer accumulate across retries (lsof -p <pid> | grep CLOSE_WAIT)Checklist
Code
Documentation & Housekeeping