Skip to content

fix(telegramSafe): exempt getUpdates and lifecycle calls from 15s tim… - #128

Merged
gamblecodezcom merged 1 commit into
mainfrom
claude/cpu-guard-script-DnD8u
Mar 4, 2026
Merged

fix(telegramSafe): exempt getUpdates and lifecycle calls from 15s tim…#128
gamblecodezcom merged 1 commit into
mainfrom
claude/cpu-guard-script-DnD8u

Conversation

@gamblecodezcom

@gamblecodezcom gamblecodezcom commented Mar 4, 2026

Copy link
Copy Markdown
Owner

User description

…eout

Telegraf's long-polling uses getUpdates with timeout=50 (Telegram holds the connection for up to 50 seconds waiting for new updates). The callApi patch was wrapping ALL methods — including getUpdates — with globalThrottle, which has a hard 15-second race. This fired on every poll cycle, throwing 'rateLimiter: API call timed out after 15000ms', causing bot.launch() to reject and systemd to enter an infinite restart loop.

Fix: add getUpdates (and Telegraf startup/lifecycle calls: getMe, deleteWebhook, setWebhook, getWebhookInfo, close, logOut) to the _MANAGED bypass set so they call the original callApi directly without the timeout wrapper.

All 60 unit tests pass. Bot loads cleanly in CI mode.

https://claude.ai/code/session_01WpEqAiDbpqnBywMjYXJHf4

Summary by Sourcery

Bug Fixes:

  • Bypass the global API timeout wrapper for getUpdates and startup/lifecycle methods so Telegraf long-polling no longer times out and crashes the bot.

CodeAnt-AI Description

Exempt long-polling and lifecycle Telegram calls from the 15s API timeout wrapper

What Changed

  • getUpdates is no longer wrapped with the global 15s timeout, so Telegraf long-polling (timeout=50) can hold connections up to 50s without being cancelled
  • Startup and lifecycle calls (getMe, deleteWebhook, setWebhook, getWebhookInfo, close, logOut) bypass the global rate-limit queue so they won't be blocked behind long-polling slots
  • callApi continues to protect other API methods but skips the wrapper for individually-managed or long-poll/lifecycle methods

Impact

✅ Bot no longer crashes or fails to launch due to getUpdates timing out
✅ Fewer unexpected restarts under systemd when using long-polling
✅ Startup/lifecycle operations complete without being delayed by long-poll requests

💡 Usage Guide

Checking Your Pull Request

Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.

Talking to CodeAnt AI

Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

Preserve Org Learnings with CodeAnt

You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

Check Your Repository Health

To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Optimized API request handling to improve responsiveness during application startup and long-polling operations, eliminating unnecessary processing delays.

…eout

Telegraf's long-polling uses getUpdates with timeout=50 (Telegram holds
the connection for up to 50 seconds waiting for new updates). The callApi
patch was wrapping ALL methods — including getUpdates — with globalThrottle,
which has a hard 15-second race. This fired on every poll cycle, throwing
'rateLimiter: API call timed out after 15000ms', causing bot.launch() to
reject and systemd to enter an infinite restart loop.

Fix: add getUpdates (and Telegraf startup/lifecycle calls: getMe,
deleteWebhook, setWebhook, getWebhookInfo, close, logOut) to the _MANAGED
bypass set so they call the original callApi directly without the timeout
wrapper.

All 60 unit tests pass. Bot loads cleanly in CI mode.

https://claude.ai/code/session_01WpEqAiDbpqnBywMjYXJHf4
@codeant-ai

codeant-ai Bot commented Mar 4, 2026

Copy link
Copy Markdown

CodeAnt AI is reviewing your PR.


Thanks for using CodeAnt! 🎉

We're free for open-source projects. if you're enjoying it, help us grow by sharing.

Share on X ·
Reddit ·
LinkedIn

@gamblecodezcom
gamblecodezcom merged commit f72e96d into main Mar 4, 2026
3 checks passed
@gamblecodezcom
gamblecodezcom deleted the claude/cpu-guard-script-DnD8u branch March 4, 2026 13:01
@sourcery-ai

sourcery-ai Bot commented Mar 4, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts the Telegraf telegramSafe callApi wrapper to bypass global throttling/timeout for getUpdates and specific lifecycle methods so long-polling and startup calls are not subject to the 15s timeout race.

Sequence diagram for telegramSafe callApi wrapping and managed method bypass

sequenceDiagram
    actor Operator
    participant Bot as TelegrafBot
    participant TelegramSafe as telegramSafe_init
    participant Tg as Telegraf_tg
    participant Throttle as globalThrottle
    participant TelegramAPI

    Operator->>Bot: bot.launch()
    Bot->>TelegramSafe: init(bot)
    TelegramSafe->>Tg: wrap tg.callApi(method, data, signal)

    Note over Bot,Tg: Case 1: managed method (e.g. getUpdates, getMe)

    Bot->>Tg: callApi(getUpdates, data, signal)
    Tg->>Tg: _MANAGED has getUpdates
    Tg->>TelegramAPI: callApi_original(getUpdates, data, signal)
    TelegramAPI-->>Tg: long-poll response (up to 50s)

    Note over Bot,Tg: Case 2: non-managed method

    Bot->>Tg: callApi(sendChatAction, data, signal)
    Tg->>Throttle: globalThrottle(sendChatAction, data, signal)
    Throttle->>TelegramAPI: callApi_original(sendChatAction, data, signal)
    TelegramAPI-->>Throttle: response
    Throttle-->>Tg: response
    Tg-->>Bot: response
Loading

File-Level Changes

Change Details Files
Bypass the globalThrottle/timeout wrapper for long-polling and lifecycle Telegram API methods so they call the original callApi directly.
  • Extend the _MANAGED Set to include getUpdates so long-polling requests are not wrapped by the 15-second timeout logic.
  • Add Telegraf lifecycle methods (getMe, deleteWebhook, setWebhook, getWebhookInfo, close, logOut) to the _MANAGED Set to avoid them being queued or timing out behind long-polling calls.
  • Clarify intent with inline comments grouping individually patched, long-poll, and lifecycle methods within the _MANAGED Set.
telegramSafe.js

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Mar 4, 2026

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5dee9e08-8932-4266-a7ed-15fd6f4b8395

📥 Commits

Reviewing files that changed from the base of the PR and between 4925afb and a3f9145.

📒 Files selected for processing (1)
  • telegramSafe.js

📝 Walkthrough

Walkthrough

Added seven methods (getUpdates, getMe, deleteWebhook, setWebhook, getWebhookInfo, close, logOut) to the _MANAGED set in telegramSafe.js to bypass the global retry and throttle wrapper layer, preventing double-wrapping and enabling direct API calls for these specific methods.

Changes

Cohort / File(s) Summary
Managed API Methods
telegramSafe.js
Added seven methods to the _MANAGED set: getUpdates, getMe, deleteWebhook, setWebhook, getWebhookInfo, close, and logOut. These methods now bypass the generic _withRetry/globalThrottle pathway to prevent double-wrapping and ensure fast-path handling for startup/lifecycle operations and long-polling.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Seven paths diverge, no longer wrapped in delay,
Fast through the startup, swift through the day,
No double-throttle here, just direct and clean,
The quickest rabbit route has ever been! 🚀

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch claude/cpu-guard-script-DnD8u

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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.

@codeant-ai codeant-ai Bot added the size:S This PR changes 10-29 lines, ignoring generated files label Mar 4, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider moving the _MANAGED set to a shared constant or helper so the list of bypassed methods is defined in one place and can be reused or tested independently from init().
  • Since the bypass list is now coupling to Telegraf’s lifecycle usage, it may be safer to group these methods (e.g., LIFECYCLE_METHODS, LONG_POLL_METHODS) or document the selection criteria in code so future additions/changes to lifecycle calls are easier to spot and update correctly.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider moving the `_MANAGED` set to a shared constant or helper so the list of bypassed methods is defined in one place and can be reused or tested independently from `init()`.
- Since the bypass list is now coupling to Telegraf’s lifecycle usage, it may be safer to group these methods (e.g., `LIFECYCLE_METHODS`, `LONG_POLL_METHODS`) or document the selection criteria in code so future additions/changes to lifecycle calls are easier to spot and update correctly.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@codeant-ai

codeant-ai Bot commented Mar 4, 2026

Copy link
Copy Markdown

Nitpicks 🔍

🔒 No security issues identified
⚡ Recommended areas for review

  • Missing retry behavior
    The new managed set causes getUpdates and the listed startup/lifecycle methods to return the original _callApi directly. That bypasses not only the globalThrottle (intended) but also the _withRetry retry wrapper — removing 429 and transient-error retries for those methods. Consider whether lifecycle calls (getMe, deleteWebhook, setWebhook, getWebhookInfo, close, logOut) should still use _withRetry (without globalThrottle) so they remain resilient to transient failures.

  • Method-name matching
    The callApi method compares the incoming method string to the entries in _MANAGED. Confirm that Telegraf always uses these exact camelCase names when invoking callApi (e.g., 'getUpdates', 'getMe', 'logOut'). If Telegraf or other callers ever pass different casing or snake_case names, the intended bypass/double-wrap prevention could fail or be over-applied.

@codeant-ai

codeant-ai Bot commented Mar 4, 2026

Copy link
Copy Markdown

CodeAnt AI finished reviewing your PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants