From d837d3f723e4c29dc932532715820648787ed3b9 Mon Sep 17 00:00:00 2001 From: x3fwy Date: Thu, 26 Mar 2026 21:24:41 +0800 Subject: [PATCH] Fix: Explicitly set proxy for httpx client to prevent connection pool corruption When running behind a proxy (e.g., Clash, V2Ray), the bot's polling loop can get stuck even if HTTP_PROXY/HTTPS_PROXY environment variables are set. The httpx connection pool becomes corrupted after network interruptions, and the bot stops responding to messages while the process is still running. This fix explicitly reads proxy from environment variables and passes it to the Application builder via builder.proxy(). --- src/bot/core.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/bot/core.py b/src/bot/core.py index 192bddd3..2d5e99bb 100644 --- a/src/bot/core.py +++ b/src/bot/core.py @@ -64,6 +64,18 @@ async def initialize(self) -> None: builder.write_timeout(30) builder.pool_timeout(30) + # Explicitly set proxy from environment variables. + # This is necessary because python-telegram-bot's Application.builder() + # does not automatically use HTTP_PROXY/HTTPS_PROXY environment variables. + # Without this, the httpx connection pool can become corrupted when running + # behind a proxy, causing the bot to stop responding to messages. + import os + + proxy_url = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") + if proxy_url: + builder.proxy(proxy_url) + logger.info("Proxy configured", proxy=proxy_url) + self.app = builder.build() # Initialize feature registry