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): cap channel auto-join time during source setup (#123838)

Tom Owers51cf0979b0fc
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/slack/slack.py b/products/warehouse_sources/backend/temporal/data_imports/sources/slack/slack.py
index 98aad2ac1949..ac5b994534ab 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/slack/slack.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/slack/slack.py
@@ -1,8 +1,9 @@
+import time
import hashlib
import datetime
import dataclasses
from collections.abc import AsyncIterable, Callable, Iterable, Iterator
-from typing import Any, Optional
+from typing import Any, Optional, cast
from django.core.cache import cache
@@ -13,6 +14,7 @@ import structlog
from asgiref.sync import async_to_sync
from requests import Request, Response
from tenacity import RetryCallState, retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitter
+from tenacity.stop import stop_base
from posthog.egress.slack.transport import slack_request
@@ -376,25 +378,47 @@ def get_channels(
]
+class _StopBeforeDeadline(stop_base):
+ def __init__(self, deadline: float) -> None:
+ self.deadline = deadline
+
+ def __call__(self, retry_state: RetryCallState) -> bool:
+ return time.monotonic() + (retry_state.upcoming_sleep or 0) >= self.deadline
+
+
# conversations.join errors that mean "nothing to do for this channel" rather than a real failure.
_SKIP_JOIN_ERRORS = {"already_in_channel", "is_archived", "method_not_supported_for_channel_type"}
-def _join_public_channels(access_token: str, channels: list[dict[str, Any]]) -> int:
+def _join_public_channels(access_token: str, channels: list[dict[str, Any]], deadline: float | None = None) -> int:
"""Join every public channel the bot isn't already in, so their message events reach the webhook.
``conversations.join`` only works for public channels — private channels must be invited manually.
Joins are idempotent (``already_in_channel`` is a no-op) and archived channels are skipped. A
missing ``channels:join`` scope is surfaced immediately, since it means auto-join can never work.
+ Stops once ``time.monotonic()`` passes ``deadline``; a later pass joins the rest.
Returns the number of channels newly joined.
"""
url = "https://slack.com/api/conversations.join"
headers = {"Authorization": f"Bearer {access_token}"}
+ post = _slack_post
+ if deadline is not None:
+ # A rate-limited join waits out Slack's Retry-After, which alone can outlast the budget.
+ post = cast(Any, _slack_post).retry_with(stop=stop_after_attempt(5) | _StopBeforeDeadline(deadline))
joined = 0
for ch in channels:
if ch.get("is_private") or ch.get("is_archived") or ch.get("is_member"):
continue
- response = _slack_post(url, headers=headers, data={"channel": ch["id"]}, timeout=10)
+ if deadline is not None and time.monotonic() >= deadline:
+ logger.info("Slack auto-join stopped at its time budget", joined=joined)
+ break
+ try:
+ response = post(url, headers=headers, data={"channel": ch["id"]}, timeout=10)
+ except (SlackRetryableError, requests.exceptions.ConnectionError, requests.exceptions.Timeout):
+ if deadline is None:
+ raise
+ logger.info("Slack auto-join stopped before its time budget ran out", joined=joined)
+ break
response.raise_for_status()
data = response.json()
if data.get("ok"):
@@ -410,10 +434,16 @@ def _join_public_channels(access_token: str, channels: list[dict[str, Any]]) ->
return joined
-def join_public_channels(access_token: str, cache_id: str, authed_user: str | None = None) -> int:
+def join_public_channels(
+ access_token: str,
+ cache_id: str,
+ authed_user: str | None = None,
+ time_budget_seconds: float | None = None,
+) -> int:
"""Auto-join all public channels the bot isn't in yet (see ``_join_public_channels``)."""
+ deadline = time.monotonic() + time_budget_seconds if time_budget_seconds is not None else None
channels = _fetch_all_channels_cached(cache_id, access_token, authed_user)
- return _join_public_channels(access_token, channels)
+ return _join_public_channels(access_token, channels, deadline)
def _add_timestamp(msg: dict[str, Any]) -> dict[str, Any]:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/slack/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/slack/source.py
index 21483778db8f..e5a2d2f671a9 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/slack/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/slack/source.py
@@ -8,6 +8,8 @@ if TYPE_CHECKING:
from products.cdp.backend.templates.hog_function_template import HogFunctionTemplateDC
+from temporalio import activity
+
from posthog.dataclasses import frozen
from products.warehouse_sources.backend.facade.source_config import (
@@ -47,6 +49,12 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.slack.slac
)
from products.warehouse_sources.backend.types import ExternalDataSourceType
+# Joining is one rate-limited call per channel, so a large workspace can't finish in one pass. The setup
+# wizard and a manual refresh discover schemas inside a web request, which the gateway ends at 120
+# seconds. The discovery activity times out at 10 minutes and runs again every few hours.
+_REQUEST_JOIN_BUDGET_SECONDS = 30.0
+_ACTIVITY_JOIN_BUDGET_SECONDS = 300.0
+
@frozen
class SlackAccess:
@@ -382,7 +390,16 @@ Prefer a manifest? Paste this when creating the app — it wires the request URL
# Joining is idempotent and only touches public channels the bot isn't in, so running it on
# each discovery/refresh also picks up channels created since the last pass.
if config.slack_access_token and config.join_public_channels and config.join_public_channels.enabled:
- join_public_channels(access.access_token, access.cache_id, access.authed_user_id)
+ # The discovery activity runs as soon as a source is created, with the larger budget, so it
+ # joins what a request pass leaves.
+ join_public_channels(
+ access.access_token,
+ access.cache_id,
+ access.authed_user_id,
+ time_budget_seconds=_ACTIVITY_JOIN_BUDGET_SECONDS
+ if activity.in_activity()
+ else _REQUEST_JOIN_BUDGET_SECONDS,
+ )
channels = get_channels(
access.cache_id, access.access_token, access.authed_user_id, force_refresh=force_refresh
)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/slack/test_slack.py b/products/warehouse_sources/backend/temporal/data_imports/sources/slack/test_slack.py
index dbcf92006d3b..9e52907812ca 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/slack/test_slack.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/slack/test_slack.py
@@ -1,3 +1,4 @@
+import time
from typing import Any
from unittest.mock import AsyncMock, MagicMock, patch
@@ -11,6 +12,8 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.tes
RecordedRequest,
ScriptedResponse,
SourceDriver,
+ always,
+ scripted_network,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.webhook_s3 import WebhookSourceManager
from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.slack import SlackSourceConfig
@@ -660,6 +663,22 @@ class TestJoinPublicChannels:
with patch(self._POST, return_value=_make_response({"ok": False, "error": "already_in_channel"})):
assert _join_public_channels("xoxb", channels) == 0
+ def test_stops_joining_once_the_deadline_passes(self) -> None:
+ channels = [{"id": "C1"}, {"id": "C2"}]
+ with patch(self._POST, return_value=_make_response({"ok": True})) as mock_post:
+ assert _join_public_channels("xoxb", channels, deadline=time.monotonic() - 1) == 0
+
+ mock_post.assert_not_called()
+
+ def test_rate_limit_retry_does_not_wait_past_the_deadline(self) -> None:
+ channels = [{"id": "C1"}, {"id": "C2"}]
+ with scripted_network(always(ScriptedResponse(status=429, headers={"Retry-After": "60"}))) as network:
+ joined = _join_public_channels("xoxb", channels, deadline=time.monotonic() + 30)
+
+ assert joined == 0
+ assert network.requests == 1
+ assert network.elapsed < 30
+
def test_missing_scope_raises(self) -> None:
# Auto-join silently doing nothing would be worse than failing — surface the missing scope.
channels = [{"id": "C1"}]
@@ -734,6 +753,7 @@ class TestGetSchemasAutoJoin:
source.get_schemas(config, team_id=1)
mock_join.assert_called_once()
+ assert mock_join.call_args.kwargs["time_budget_seconds"] is not None
def test_toggle_can_be_explicitly_disabled(self) -> None:
from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.slack import (