Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(slack-app): retry slack rate limits when fetching thread messages (#60983)

Alessandro Pogliaghi9882c67261cf
diff --git a/products/slack_app/backend/api.py b/products/slack_app/backend/api.py
index d3d1165434ba..03a35d23c7a4 100644
--- a/products/slack_app/backend/api.py
+++ b/products/slack_app/backend/api.py
@@ -18,6 +18,7 @@ from django.views.decorators.csrf import csrf_exempt
import requests
import structlog
from slack_sdk.errors import SlackApiError
+from slack_sdk.http_retry.builtin_handlers import RateLimitErrorRetryHandler
from temporalio.common import WorkflowIDConflictPolicy, WorkflowIDReusePolicy
from posthog.llm.gateway_client import get_llm_client
@@ -945,7 +946,9 @@ def _collect_thread_messages(
slack: SlackIntegration, integration: Integration, channel: str, thread_ts: str, our_bot_id: str | None
) -> list[dict[str, str]]:
"""Fetch thread messages, strip bot mentions, and resolve user display names."""
- thread_response = slack.client.conversations_replies(channel=channel, ts=thread_ts)
+ client = slack.client
+ client.retry_handlers.append(RateLimitErrorRetryHandler(max_retry_count=3))
+ thread_response = client.conversations_replies(channel=channel, ts=thread_ts)
raw_messages: list[dict] = thread_response.get("messages", [])
user_cache: dict[str, str] = {}
diff --git a/products/slack_app/backend/tests/test_collect_thread_messages.py b/products/slack_app/backend/tests/test_collect_thread_messages.py
index c884eac270da..482f2feeea2c 100644
--- a/products/slack_app/backend/tests/test_collect_thread_messages.py
+++ b/products/slack_app/backend/tests/test_collect_thread_messages.py
@@ -2,6 +2,7 @@ import pytest
from unittest.mock import MagicMock, patch
from parameterized import parameterized
+from slack_sdk.http_retry.builtin_handlers import RateLimitErrorRetryHandler
from posthog.models.integration import Integration, SlackIntegration
from posthog.models.organization import Organization
@@ -329,7 +330,18 @@ class TestCollectThreadMessages:
assert [m["user"] for m in result] == ["PostHog", "andy"]
- def test_block_extraction_failure_does_not_break_collection(self, mock_get_user_info):
+ def test_registers_rate_limit_retry_handler(self, mock_get_user_info):
+ self.slack.client.retry_handlers = []
+ self._set_thread([{"user": "U_ANDY", "text": "hi"}])
+ mock_get_user_info.return_value = {"user": {"profile": {"display_name": "andy"}}}
+
+ _collect_thread_messages(self.slack, self.integration, "C001", "1.234", our_bot_id=None)
+
+ assert any(isinstance(h, RateLimitErrorRetryHandler) for h in self.slack.client.retry_handlers)
+
+ handlers = [h for h in self.slack.client.retry_handlers if isinstance(h, RateLimitErrorRetryHandler)]
+ assert len(handlers) == 1
+ assert handlers[0].max_retry_count == 3
# If block flattening blows up for one message, the rest of the thread still flows.
self._set_thread(
[