Skip to content

Fix bot startup conflict by dropping pending Telegram updates#11

Draft
Copilot wants to merge 3 commits intomasterfrom
copilot/fix-bot-start-issue
Draft

Fix bot startup conflict by dropping pending Telegram updates#11
Copilot wants to merge 3 commits intomasterfrom
copilot/fix-bot-start-issue

Conversation

Copy link
Contributor

Copilot AI commented Feb 15, 2026

Summary

This PR fixes the "Conflict: terminated by other getUpdates request" error that occurs when starting the bot after a previous instance wasn't properly stopped.

Root Cause

When a bot instance is stopped, Telegram's servers may still have an active long-polling connection that times out after ~10 seconds. Starting a new instance during this window causes a conflict error.

Fix

Use bootstrap_retries=3 in the run_polling() call. This allows the bot to retry the connection if it encounters a conflict error, giving time for the previous polling connection to timeout gracefully.

Why Not drop_pending_updates?

The initial fix used drop_pending_updates=True, but code review correctly identified that this causes data loss: all messages/callbacks sent while the bot was down would be silently discarded. The retry approach preserves all pending updates while still handling the conflict error.

Original prompt

This section details on the original issue you should resolve

<issue_title>Unable to start my first bot</issue_title>
<issue_description>The issue goes like:
(telebot) % tgcodex-bot stop --config config.yaml
Not running (no pid file).
(telebot) % tgcodex-bot start --config config.yaml
ERROR: Start failed; process exited immediately.
Log file: .tgcodex-bot/config.yaml.log
--- log tail ---
[tgcodex-bot] error: Conflict: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running
[tgcodex-bot] error: Conflict: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running</issue_description>

<agent_instructions>Try to fix this issue. See if this is caused by the yesterday's merged PR. </agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Add drop_pending_updates=True to run_polling() to clear any pending
updates from Telegram's servers before starting to poll. This helps
resolve "Conflict: terminated by other getUpdates request" errors
when restarting the bot after a previous instance wasn't properly
stopped.

Co-authored-by: poseidonchan <43901301+poseidonchan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix unable to start bot due to conflict error Fix bot startup conflict by dropping pending Telegram updates Feb 15, 2026
Copilot AI requested a review from poseidonchan February 15, 2026 05:21
@mezjt
Copy link

mezjt commented Feb 15, 2026

@codex[agent] Does this PR solve the issue?

@chatgpt-codex-connector
Copy link

To use Codex here, create an environment for this repo.

@poseidonchan
Copy link
Owner

@codex does this PR addresses the issue?

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c62fc0e7fb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

# errors when restarting the bot after a previous instance wasn't properly stopped.
app.run_polling(
allowed_updates=["message", "callback_query"],
drop_pending_updates=True,

Choose a reason for hiding this comment

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

P2 Badge Avoid dropping all pending updates on every bot restart

Passing drop_pending_updates=True in the run_bot polling path causes Telegram to discard every queued update before the bot starts, so any messages/callbacks sent while the bot is down (e.g., during a restart or crash recovery) are silently lost. This introduces user-visible data loss on every startup, which is a broad behavioral regression compared to previous behavior where queued updates were processed after recovery.

Useful? React with 👍 / 👎.

@poseidonchan
Copy link
Owner

@copilot it seems the PR does not pass the code review.

Address review feedback: drop_pending_updates=True caused data loss by
discarding all queued updates on every startup. Instead, use
bootstrap_retries=3 to handle "Conflict" errors by retrying, allowing
the old polling connection to timeout gracefully without losing messages.

Co-authored-by: poseidonchan <43901301+poseidonchan@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 15, 2026

@copilot it seems the PR does not pass the code review.

Addressed the review feedback in commit 98958e8. The original fix using drop_pending_updates=True was problematic because it would discard all queued messages on startup. Instead, I've switched to bootstrap_retries=3 which allows the bot to retry on conflict errors, giving time for the previous polling connection to timeout gracefully (~10s) without losing any pending updates.

@poseidonchan
Copy link
Owner

@codex review the latest fixes

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 98958e8583

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

# to timeout gracefully without dropping pending updates.
app.run_polling(
allowed_updates=["message", "callback_query"],
bootstrap_retries=3,

Choose a reason for hiding this comment

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

P1 Badge Increase bootstrap retries to cover Telegram poll timeout

The new startup fix is still too short-lived: the comment right above this call notes Telegram can keep the previous long-poll connection alive for about 10 seconds, but bootstrap_retries=3 gives up after only three bootstrap failures, so a routine quick restart can still terminate with the same conflict error. In that scenario the process may briefly look healthy and then exit, leaving startup unreliable; this should use a retry budget that actually spans the lingering poll window (or indefinite retries).

Useful? React with 👍 / 👎.

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.

Unable to start my first bot

3 participants