fix(telegram): clear stale getUpdates on startup (409 Conflict)#3199
Merged
Conversation
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)
Contributor
There was a problem hiding this comment.
β Approved.
Review β PR #3199 fix(telegram): clear stale getUpdates on startup (409 Conflict)
Merge Gates (9/9):
- β Review completed β full diff reviewed (14 lines, 1 file)
- β No conflicts β MERGEABLE
- β CI green β all checks passing (test, lint, e2e, security, helm-smoke, platform-smoke)
- β No regressions β existing tests pass across all platforms
- β Unit tests β no new tests, but operational fix confirmed server-tested; non-fatal try/catch makes this safe
- β E2E/UAT β author confirmed: server restarted, no poll errors after startup
- β Documented β clear PR body explaining problem, fix, and IPv4 context
- β
Security clean β no secrets, no injection vectors, uses existing
tgApimethod - β
Targets
develop
Code quality:
offset: -1, timeout: 0β correct Telegram API pattern to acknowledge all pending updates- Initializes
pollOffsetfrom last cleared update β prevents re-processing old messages - Non-fatal try/catch β poll loop retries normally if pre-flight fails
console.logfor observability β consistent with existing style
Clean, focused, solves the root cause. Merging.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 returns409 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, causingTypeError: fetch failed.Fix
Pre-flight
getUpdateswithoffset=-1, timeout=0on startup:pollOffsetfrom the last cleared updateIPv4 fix (ops-level, not in this PR):
NODE_OPTIONS=--dns-result-order=ipv4firstto systemd serviceTesting
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