Skip to content

fix(telegram): clear stale getUpdates on startup (409 Conflict)#3199

Merged
aegis-gh-agent[bot] merged 2 commits into
developfrom
fix/telegram-poll-stale-connection
May 11, 2026
Merged

fix(telegram): clear stale getUpdates on startup (409 Conflict)#3199
aegis-gh-agent[bot] merged 2 commits into
developfrom
fix/telegram-poll-stale-connection

Conversation

@OneStepAt4time

Copy link
Copy Markdown
Owner

Problem

On server restart, the Telegram bot's long-poll connection may still be active on Telegram's side. When the new instance calls getUpdates, Telegram returns 409 Conflict: terminated by other getUpdates request. This cascades into repeated poll failures.

Additionally, Node.js tries IPv6 first for api.telegram.org, and IPv6 is broken on this server. The IPv6 timeout fires the AbortSignal before IPv4 fallback completes, causing TypeError: fetch failed.

Fix

Pre-flight getUpdates with offset=-1, timeout=0 on startup:

  • Clears any pending updates from the previous instance
  • Initializes pollOffset from the last cleared update
  • Non-fatal: if pre-flight fails, the poll loop retries normally

IPv4 fix (ops-level, not in this PR):

  • Add NODE_OPTIONS=--dns-result-order=ipv4first to systemd service

Testing

Server restarted with this code β€” no poll errors after startup. Previously, errors appeared within 10 minutes.

Gate: tsc βœ“, build βœ“

Fixes the 409 Conflict root cause

On startup, send a getUpdates request with offset=-1 and timeout=0
to clear any pending updates from a previous bot instance. This
prevents the 409 Conflict error that occurs when the server restarts
while a long-poll connection is still active on Telegram's side.

Also initializes pollOffset from the last cleared update to avoid
re-processing old messages.

Related: IPv6 connectivity issue (NODE_OPTIONS=--dns-result-order=ipv4first)

@aegis-gh-agent aegis-gh-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

βœ… Approved.

Review β€” PR #3199 fix(telegram): clear stale getUpdates on startup (409 Conflict)

Merge Gates (9/9):

  1. βœ… Review completed β€” full diff reviewed (14 lines, 1 file)
  2. βœ… No conflicts β€” MERGEABLE
  3. βœ… CI green β€” all checks passing (test, lint, e2e, security, helm-smoke, platform-smoke)
  4. βœ… No regressions β€” existing tests pass across all platforms
  5. βœ… Unit tests β€” no new tests, but operational fix confirmed server-tested; non-fatal try/catch makes this safe
  6. βœ… E2E/UAT β€” author confirmed: server restarted, no poll errors after startup
  7. βœ… Documented β€” clear PR body explaining problem, fix, and IPv4 context
  8. βœ… Security clean β€” no secrets, no injection vectors, uses existing tgApi method
  9. βœ… Targets develop

Code quality:

  • offset: -1, timeout: 0 β€” correct Telegram API pattern to acknowledge all pending updates
  • Initializes pollOffset from last cleared update β€” prevents re-processing old messages
  • Non-fatal try/catch β€” poll loop retries normally if pre-flight fails
  • console.log for observability β€” consistent with existing style

Clean, focused, solves the root cause. Merging.

@aegis-gh-agent aegis-gh-agent Bot merged commit 75cc56a into develop May 11, 2026
17 checks passed
@aegis-gh-agent aegis-gh-agent Bot deleted the fix/telegram-poll-stale-connection branch May 11, 2026 12:21
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.

1 participant