fix(telegram): release fallback transport pools on connect failure (salvage of #71593 by @aneym) - #72359
Merged
Conversation
The per-IP httpx transports were built once in __init__ and never torn down. A connect that reached ESTABLISHED and was then closed by the peer left its socket in CLOSE_WAIT inside the pool, and the failure path only logged and continued — so the poisoned pool was retained and leaked one descriptor per retry. With DNS for api.telegram.org failing, every poll fell through to the seed IP and leaked another fd every ~2.5s. The bot gateway reached 177 CLOSE_WAIT sockets against launchd's 256 soft limit and wedged: accept() on the gateway port, config reads and DNS resolution all failed with EMFILE, which in turn made the primary path fail and fed the loop. Build fallback transports lazily and discard them on a retryable connect failure, and bound every pool at 8 connections (httpx defaults to 100, so two seed IPs plus primary could alone exceed the fd ceiling). Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
The salvaged fix (#71593) rebuilds Telegram fallback pools lazily and discards+aclose()s a pool on retryable connect failure (_reset_fallback), bounding each at Limits(max_connections=8) as a setdefault default. The PR shipped no test. Add tests/gateway/test_telegram_fallback_pool_release_71593.py: * failed fallback pool is aclose()d and dropped from _fallbacks (the discard-on-failure path — reverting the _reset_fallback call fails it) * a recovered pool is retained, only the failed one discarded * _reset_fallback is a no-op when the pool was never built * caller-supplied limits win over the _POOL_LIMITS setdefault default * the max_connections=8 default applies when the caller omits limits Update the eager-build assumptions in test_telegram_network.py to the new lazy contract (fallbacks materialize via _get_fallback, not in __init__).
Contributor
૮ >ﻌ< ა ci reviewran on 925a304 all good! |
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
The Telegram fallback transport no longer leaks a file descriptor per failed connect, which previously accumulated CLOSE_WAIT sockets until EMFILE wedged the entire gateway.
Root cause:
TelegramFallbackTransportbuilt per-IPhttpxpools eagerly and, on a retryable connect failure, only logged andcontinued — retaining the poisoned pool. The author observed 177 CLOSE_WAIT sockets against launchd's 256 soft limit.Changes
plugins/platforms/telegram/telegram_network.py: build fallback pools lazily; on retryable failure_reset_fallbackpops +aclose()s the pool;Limits(max_connections=8)added as a default viasetdefault(caller-supplied limits still win); lock-safeaclose()._reset_fallbackfails the test).Validation
5 tests passed; production-verified 0 CLOSE_WAIT after fix.
Salvage of #71593 — @aneym's commit cherry-picked with authorship preserved; regression test added as a maintainer follow-up.
Infographic