fix(telegram): prevent duplicate messages on send timeout#3922
Closed
dlkakbs wants to merge 1 commit into
Closed
Conversation
When sendMessage times out, the Bot API may have already delivered the message even though the HTTP client got no response. PR NousResearch#3288 added _send_with_retry() which retried on any transient error (including timeouts), stacking on top of TelegramAdapter.send()'s existing 3- attempt internal loop — risking 2–3 duplicate messages per response. - Add SendResult.delivery_uncertain flag; when True, _send_with_retry() returns immediately without retrying or falling back to plain text. - Add TelegramAdapter._looks_like_send_timeout() to detect TimedOut / ReadTimeout / WriteTimeout exceptions (with and without the python-telegram-bot import). - Set delivery_uncertain=True in send()'s final except clause when the exhausted error is a send timeout. Fixes NousResearch#3906.
4 tasks
Contributor
Author
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
Closes #3906.
PR #3288 added
_send_with_retry()toBasePlatformAdapter, which retries on transient errors including timeouts.TelegramAdapter.send()already has its own internal 3-attempt retry loop for network errors. This created two stacked retry layers.The critical issue: a Telegram
sendMessagetimeout is ambiguous — the Bot API may have already accepted and delivered the message even though the HTTP client got no response. When either retry layer fired after such a timeout, it re-sent the same content, producing 2–3 duplicate messages.Fix
gateway/platforms/base.pySendResult.delivery_uncertain: bool = Falsefield_send_with_retry(), return immediately (no retry, no plain-text fallback) whenresult.delivery_uncertain is Truegateway/platforms/telegram.py_looks_like_send_timeout()static method — detectsTimedOut,ReadTimeout,WriteTimeoutby isinstance check and by class name, so it works with and without thepython-telegram-botimportsend(), setdelivery_uncertain=Truewhen the final caught exception is a send timeoutWhat is NOT changed
The internal
for _send_attempt in range(3)loop insidesend()retries onNetworkError(includingTimedOut). This pre-existed PR #3288 and is out of scope for this fix.Tests
tests/gateway/test_telegram_send_timeout.py— 8 new tests:_looks_like_send_timeout()detectsTimedOutinstance, class-name matches (ReadTimeout,WriteTimeout), and correctly ignores non-timeoutNetworkErrorsend()setsdelivery_uncertain=Trueon timeout,Falseon other errors_send_with_retry()callssend()exactly once whendelivery_uncertain=True_send_with_retry()still retries normally on non-uncertain network errorsAll 8 new tests pass. All 102 existing Telegram tests unaffected.