Skip to content

Increase Telegram request timeouts (connect, pool, write) to fix TimedOut errors#164

Merged
ovchynnikov merged 3 commits intoovchynnikov:mainfrom
avelytchko:main
Mar 13, 2026
Merged

Increase Telegram request timeouts (connect, pool, write) to fix TimedOut errors#164
ovchynnikov merged 3 commits intoovchynnikov:mainfrom
avelytchko:main

Conversation

@avelytchko
Copy link
Copy Markdown
Contributor

@avelytchko avelytchko commented Mar 13, 2026

  • Added TELEGRAM_CONNECT_TIMEOUT=60 and TELEGRAM_POOL_TIMEOUT=30
  • Set default timeouts for all requests via HTTPXRequest(connect_timeout, pool_timeout, read_timeout, write_timeout)
  • Explicitly passed connect_timeout to send_video / send_photo / send_media_group calls for consistency
  • Prevents TimedOut / ConnectTimeout errors during LLM replies and rate-limit messages

Summary by CodeRabbit

  • Chores
    • Optimized timeout configuration for Telegram API communications to enhance stability and reliability.

- Added TELEGRAM_CONNECT_TIMEOUT=30 and TELEGRAM_POOL_TIMEOUT=10
- Set default timeouts for all requests via HTTPXRequest(connect_timeout, pool_timeout, read_timeout, write_timeout)
- Explicitly passed connect_timeout to send_video / send_photo / send_media_group calls for consistency
- Prevents TimedOut / ConnectTimeout errors during LLM replies and rate-limit messages
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 13, 2026

Walkthrough

Refactors Telegram API request handling in src/main.py by introducing HTTPXRequest with explicit timeout constants (connect, pool, read, write) instead of ad-hoc timeout usage. Consolidates timeout configuration into dedicated constants and applies them through a per-application request object passed to Application.builder().

Changes

Cohort / File(s) Summary
Telegram Request Configuration
src/main.py
Introduces HTTPXRequest with explicit timeout constants (TELEGRAM_CONNECT_TIMEOUT, TELEGRAM_POOL_TIMEOUT, TELEGRAM_READ_TIMEOUT, TELEGRAM_WRITE_TIMEOUT). Updates main() to construct HTTPXRequest and pass it to Application.builder().request(). Applies timeouts to send_video, send_media_group, and send_photo operations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: increasing Telegram request timeouts to address TimedOut errors, which aligns with the PR's core objective of preventing timeout-related failures.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can use your project's `ruff` configuration to improve the quality of Python code reviews.

Add a Ruff configuration file to your project to customize how CodeRabbit runs ruff.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
src/main.py (2)

414-417: Redundant timeout specification.

Since HTTPXRequest is now configured with these timeout defaults (lines 957-962), explicitly passing them on each API call is redundant—per-call values override the defaults. Consider removing the per-call timeouts to centralize configuration, or remove the HTTPXRequest timeouts if per-call control is intentional.

♻️ Option A: Remove per-call timeouts (rely on HTTPXRequest defaults)
                 await update.message.chat.send_video(
                     video=video_file,
                     width=width,
                     height=height,
                     has_spoiler=has_spoiler,
                     disable_notification=True,
-                    connect_timeout=TELEGRAM_CONNECT_TIMEOUT,
-                    write_timeout=TELEGRAM_WRITE_TIMEOUT,
-                    read_timeout=TELEGRAM_READ_TIMEOUT,
                     reply_to_message_id=update.message.message_id,
                 )
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main.py` around lines 414 - 417, Remove the redundant per-call timeout
arguments (connect_timeout, write_timeout, read_timeout) from the API call where
reply_to_message_id=update.message.message_id is passed so the call uses the
centralized HTTPXRequest defaults configured in HTTPXRequest (lines ~957-962);
locate the call site in src/main.py that includes those timeout kwargs and
simply delete those three parameters (leave reply_to_message_id intact).

51-54: Discrepancy with PR description: env vars vs hardcoded constants.

The PR description states these should be environment variables (TELEGRAM_CONNECT_TIMEOUT=30 and TELEGRAM_POOL_TIMEOUT=10), but the implementation uses hardcoded constants with different values (60 and 30). Consider making these configurable via os.getenv() for runtime flexibility, consistent with other settings in this file.

♻️ Proposed fix to use environment variables
-TELEGRAM_CONNECT_TIMEOUT = 60
-TELEGRAM_POOL_TIMEOUT = 30
-TELEGRAM_READ_TIMEOUT = 120
-TELEGRAM_WRITE_TIMEOUT = 120
+TELEGRAM_CONNECT_TIMEOUT = int(os.getenv("TELEGRAM_CONNECT_TIMEOUT", "60"))
+TELEGRAM_POOL_TIMEOUT = int(os.getenv("TELEGRAM_POOL_TIMEOUT", "30"))
+TELEGRAM_READ_TIMEOUT = int(os.getenv("TELEGRAM_READ_TIMEOUT", "120"))
+TELEGRAM_WRITE_TIMEOUT = int(os.getenv("TELEGRAM_WRITE_TIMEOUT", "120"))
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main.py` around lines 51 - 54, The hardcoded timeout constants
TELEGRAM_CONNECT_TIMEOUT and TELEGRAM_POOL_TIMEOUT conflict with the PR
description and should be made configurable via environment variables; update
their assignments (and ideally TELEGRAM_READ_TIMEOUT and TELEGRAM_WRITE_TIMEOUT
for consistency) to use os.getenv with integer conversion and sane defaults
(TELEGRAM_CONNECT_TIMEOUT default 30, TELEGRAM_POOL_TIMEOUT default 10, keep or
set defaults for READ/WRITE as before) while validating/converting the env
values to ints before assigning to the constants used throughout the module.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/main.py`:
- Around line 414-417: Remove the redundant per-call timeout arguments
(connect_timeout, write_timeout, read_timeout) from the API call where
reply_to_message_id=update.message.message_id is passed so the call uses the
centralized HTTPXRequest defaults configured in HTTPXRequest (lines ~957-962);
locate the call site in src/main.py that includes those timeout kwargs and
simply delete those three parameters (leave reply_to_message_id intact).
- Around line 51-54: The hardcoded timeout constants TELEGRAM_CONNECT_TIMEOUT
and TELEGRAM_POOL_TIMEOUT conflict with the PR description and should be made
configurable via environment variables; update their assignments (and ideally
TELEGRAM_READ_TIMEOUT and TELEGRAM_WRITE_TIMEOUT for consistency) to use
os.getenv with integer conversion and sane defaults (TELEGRAM_CONNECT_TIMEOUT
default 30, TELEGRAM_POOL_TIMEOUT default 10, keep or set defaults for
READ/WRITE as before) while validating/converting the env values to ints before
assigning to the constants used throughout the module.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 27755bf3-4c8a-499d-88d1-2634ff16cfaa

📥 Commits

Reviewing files that changed from the base of the PR and between b53a7d6 and ad1de83.

📒 Files selected for processing (1)
  • src/main.py

@ovchynnikov ovchynnikov merged commit 648a399 into ovchynnikov:main Mar 13, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants