Skip to content

fix: prevent double Telegram polling loop on startup#62

Open
bluzername wants to merge 1 commit intomoazbuilds:masterfrom
bluzername:fix/telegram-double-polling-guard
Open

fix: prevent double Telegram polling loop on startup#62
bluzername wants to merge 1 commit intomoazbuilds:masterfrom
bluzername:fix/telegram-double-polling-guard

Conversation

@bluzername
Copy link
Copy Markdown

Problem

The Telegram bot daemon opens two concurrent connections to api.telegram.org on startup, which causes getUpdates to return 409 Conflict on every poll. Messages still work through retries but the logs are spammed with error messages nonstop.

I had this happening on one of my agents and the logs were full of 409 errors which made it hard to see the real activity.

Root Cause

startPolling() in src/commands/telegram.ts has no guard against being called twice. If initTelegram() is invoked twice before the first poll() enters its while (running) loop, two polling loops start concurrently - each opening its own TCP connection to Telegram API.

Fix

Added an isPolling flag that prevents startPolling() from running more than once:

let isPolling = false;

export function startPolling(debug = false): void {
  if (isPolling) return;  // already running, skip
  isPolling = true;
  // ...
}

The flag is also reset to false in the catch handler so if the first attempt fails completely, a retry can still work.

This is the exact fix suggested in the issue. Simple, no side effects, covers the race condition.

Closes #47

@moazbuilds
Copy link
Copy Markdown
Owner

@claude

@claude
Copy link
Copy Markdown

claude bot commented Mar 26, 2026

Claude Code is working…

I'll analyze this and get back to you.

View job run

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.

Telegram 409 Conflict — double polling loop on startup

2 participants