Skip to content

Commit 73f32fd

Browse files
bibotaix3fwy
andauthored
Fix: Explicitly set proxy for httpx client to prevent connection pool corruption (#166)
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(). Co-authored-by: x3fwy <x3fwy@users.noreply.github.com>
1 parent ba5a990 commit 73f32fd

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/bot/core.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ async def initialize(self) -> None:
6464
builder.write_timeout(30)
6565
builder.pool_timeout(30)
6666

67+
# Explicitly set proxy from environment variables.
68+
# This is necessary because python-telegram-bot's Application.builder()
69+
# does not automatically use HTTP_PROXY/HTTPS_PROXY environment variables.
70+
# Without this, the httpx connection pool can become corrupted when running
71+
# behind a proxy, causing the bot to stop responding to messages.
72+
import os
73+
74+
proxy_url = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY")
75+
if proxy_url:
76+
builder.proxy(proxy_url)
77+
logger.info("Proxy configured", proxy=proxy_url)
78+
6779
self.app = builder.build()
6880

6981
# Initialize feature registry

0 commit comments

Comments
 (0)