Skip to content

fix(slack): Handle Slack API rate limits for bookmarks.add - #284

Draft
sentry[bot] wants to merge 3 commits into
mainfrom
seer/fix-slack-bookmark-ratelimit
Draft

fix(slack): Handle Slack API rate limits for bookmarks.add#284
sentry[bot] wants to merge 3 commits into
mainfrom
seer/fix-slack-bookmark-ratelimit

Conversation

@sentry

@sentry sentry Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

This PR addresses the issue where Slack API bookmarks.add calls were failing due to too_many_requests errors, particularly during rapid API interactions (e.g., incident creation).

Changes:

  • Configured the slack_sdk.WebClient to use RateLimitErrorRetryHandler with a max_retry_count of 1. This enables automatic retries with exponential backoff for all Slack API calls when a 429 (Too Many Requests) response is received.
  • Added specific retry logic within the add_bookmark method to handle too_many_requests errors. This logic reads the Retry-After header from Slack's response, pauses for the specified duration, and retries the bookmarks_add call once. This mirrors the existing retry pattern for not_in_channel errors.

Reasoning:
The previous implementation lacked a mechanism to handle Slack API rate limits, causing bookmark creation to fail when multiple Slack operations occurred in quick succession. These changes ensure that bookmarks.add operations are resilient to temporary rate limiting, improving the reliability of incident setup.

Fixes FIRETOWER-BACKEND-7D

Comment @sentry <feedback> on this PR to have Autofix iterate on the changes.

Co-authored-by: richard.gibert@sentry.io richard.gibert@sentry.io

@sentry
sentry Bot requested a review from a team as a code owner July 14, 2026 15:01
@rgibert
rgibert enabled auto-merge (squash) July 14, 2026 15:03
@rgibert
rgibert disabled auto-merge July 14, 2026 15:03
Comment thread src/firetower/integrations/services/slack.py Outdated
Comment thread src/firetower/integrations/services/slack.py
Comment thread src/firetower/integrations/services/slack.py Outdated
Comment thread src/firetower/integrations/services/slack.py
Comment thread src/firetower/integrations/services/slack.py
Move time import to stdlib group and remove blank line between
django and slack_sdk imports to satisfy ruff I001.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Agent transcript: https://claudescope.sentry.dev/share/JJLaAbC0ZHY0YYYPKBGFhk0eyIGNWtfQ6dTCmF4_6ok
Comment thread src/firetower/integrations/services/slack.py
Comment thread src/firetower/integrations/services/slack.py
Fix WebClient | None type annotation to satisfy mypy assignment check.
Change error string from "too_many_requests" to "ratelimited" to match
the actual Slack API error code for rate limit responses.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Agent transcript: https://claudescope.sentry.dev/share/HQRizc5I0UMzEKdstTt4ZI3AVyZd2Hh47PTo399R0wE
Comment on lines +398 to +408
time.sleep(retry_after)
try:
self.client.bookmarks_add(
channel_id=channel_id, title=title, type="link", link=link
)
return True
except SlackApiError as retry_error:
logger.error(
f"Error adding bookmark after rate limit retry: {retry_error}",
extra={"channel_id": channel_id},
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The manual retry logic in add_bookmark combined with the SDK's RateLimitErrorRetryHandler creates duplicate retries, causing unintended, extended blocking on rate-limiting errors.
Severity: MEDIUM

Suggested Fix

Remove the manual retry logic within the add_bookmark function. Rely solely on the SDK's RateLimitErrorRetryHandler for handling rate-limiting errors. Consider increasing the max_retry_count on the handler if more than one automatic retry is desired.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: src/firetower/integrations/services/slack.py#L393-L408

Potential issue: The `add_bookmark` function implements manual retry logic for
`"ratelimited"` errors, but the Slack client is also configured with a
`RateLimitErrorRetryHandler`. When a rate limit error (429) occurs, the SDK handler
retries once. If that also fails, the manual logic catches the error and retries by
calling `self.client.bookmarks_add()` again. This new call is treated as a fresh attempt
by the SDK, so its handler will retry again if it encounters another 429. This
interaction results in compounded retries and can block the synchronous function for up
to three times the `Retry-After` duration, impacting performance during incident
creation.

Also affects:

  • src/firetower/integrations/services/slack.py:68~69

@rgibert
rgibert marked this pull request as draft July 14, 2026 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant