Skip to content

The agent that grows with you

fix(gateway): cap the inline server retry_after wait; return the typed failure past 60s

kshitijk4poordfffd0ee1ec7
diff --git a/gateway/platforms/base.py b/gateway/platforms/base.py
index 4d978497219..b16bb0e9078 100644
--- a/gateway/platforms/base.py
+++ b/gateway/platforms/base.py
@@ -1660,6 +1660,11 @@ class SendResult:
error_kind: Optional[str] = None
+# Longest server ``retry_after`` ``_send_with_retry`` will sleep inline. Longer penalties return the
+# typed failure so the delivery ledger owns the wait (#91969: a 97-minute FloodWait slept verbatim
+# pinned the send coroutine and froze inbound on every platform).
+_SEND_RETRY_INLINE_WAIT_CAP_SECS = 60.0
+
# Platform-neutral send-failure kinds for ``SendResult.error_kind``: too_long (size cap),
# bad_format (markup rejected; plain-text retry fixes), forbidden (the bot CANNOT reach the user),
# not_found (chat/thread/message gone), rate_limited, transient (connection-level, retry-safe),
@@ -3254,6 +3259,16 @@ class BasePlatformAdapter(ABC):
backoff = server_retry_after
if backoff is None:
backoff = base_delay * (2 ** (attempt - 1))
+ elif backoff > _SEND_RETRY_INLINE_WAIT_CAP_SECS:
+ # Never hold this coroutine open for a long server penalty: a 97-minute
+ # FloodWait slept verbatim once froze inbound on every platform (#91969).
+ # Return the typed failure; the delivery ledger redelivers after the cooldown.
+ logger.error(
+ "[%s] Server asked to retry after %.0fs (> %.0fs inline cap); returning "
+ "typed failure for redelivery instead of sleeping: %s",
+ self.name, backoff, _SEND_RETRY_INLINE_WAIT_CAP_SECS, error_str,
+ )
+ return result
delay = backoff + random.uniform(0, 1)
server_retry_after = None
logger.warning("[%s] Send failed (attempt %d/%d, retrying in %.1fs): %s", self.name,
diff --git a/tests/gateway/test_send_retry.py b/tests/gateway/test_send_retry.py
index 4e13581dfe9..f893d0b3ebe 100644
--- a/tests/gateway/test_send_retry.py
+++ b/tests/gateway/test_send_retry.py
@@ -81,25 +81,19 @@ class TestIsTimeoutError:
# ---------------------------------------------------------------------------
class TestIsRateLimitedError:
- def test_none_is_not_rate_limited(self):
- assert not _StubAdapter._is_rate_limited_error(None)
-
- def test_empty_is_not_rate_limited(self):
- assert not _StubAdapter._is_rate_limited_error("")
-
- def test_flood_is_rate_limited(self):
- assert _StubAdapter._is_rate_limited_error("flood control exceeded, retry in 60 seconds")
-
- def test_too_many_requests_is_rate_limited(self):
- assert _StubAdapter._is_rate_limited_error("Too Many Requests: rate limit reached")
-
- def test_weixin_rate_limit_text_is_rate_limited(self):
- assert _StubAdapter._is_rate_limited_error(
- "iLink sendmessage rate limited; cooldown active for 42.0s"
- )
-
- def test_formatting_error_not_rate_limited(self):
- assert not _StubAdapter._is_rate_limited_error("Bad Request: can't parse entities")
+ @pytest.mark.parametrize(
+ ("error", "rate_limited"),
+ [
+ (None, False),
+ ("", False),
+ ("Bad Request: can't parse entities", False),
+ ("flood control exceeded, retry in 60 seconds", True),
+ # Platforms without a retry_after field (Weixin) surface a bare text.
+ ("iLink sendmessage rate limited; cooldown active for 42.0s", True),
+ ],
+ )
+ def test_rate_limited_follows_the_shared_classifier(self, error, rate_limited):
+ assert _StubAdapter._is_rate_limited_error(error) is rate_limited
# ---------------------------------------------------------------------------
@@ -261,6 +255,21 @@ class TestSendWithRetryRateLimited:
# No plain-text fallback content on either call
assert all("plain text" not in c[1].lower() for c in adapter._send_calls)
+ @pytest.mark.asyncio
+ async def test_long_server_retry_after_returns_typed_failure_without_sleeping(self):
+ """A retry_after past the inline cap must not pin the coroutine (#91969): the
+ typed failure goes back to the delivery ledger, no sleep, no fallback, no notice."""
+ adapter = _StubAdapter()
+ adapter._send_results = [
+ SendResult(success=False, error="flood_control:5820", retry_after=5820.0),
+ ]
+ with patch("asyncio.sleep", new_callable=AsyncMock) as mock_sleep:
+ result = await adapter._send_with_retry("c", "hello")
+ assert result.success is False
+ assert result.retry_after == 5820.0
+ mock_sleep.assert_not_called()
+ assert len(adapter._send_calls) == 1 # the original send only
+
@pytest.mark.asyncio
async def test_rate_limited_exhausted_returns_typed_failure_not_fallback(self):
"""When retries on a rate-limited send are exhausted, return the typed