fix(slack): Handle Slack API rate limits for bookmarks.add - #284
Draft
sentry[bot] wants to merge 3 commits into
Draft
fix(slack): Handle Slack API rate limits for bookmarks.add#284sentry[bot] wants to merge 3 commits into
sentry[bot] wants to merge 3 commits into
Conversation
rgibert
enabled auto-merge (squash)
July 14, 2026 15:03
rgibert
disabled auto-merge
July 14, 2026 15:03
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
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}, | ||
| ) |
Contributor
Author
There was a problem hiding this comment.
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
marked this pull request as draft
July 14, 2026 19:38
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.
This PR addresses the issue where Slack API
bookmarks.addcalls were failing due totoo_many_requestserrors, particularly during rapid API interactions (e.g., incident creation).Changes:
slack_sdk.WebClientto useRateLimitErrorRetryHandlerwith amax_retry_countof 1. This enables automatic retries with exponential backoff for all Slack API calls when a 429 (Too Many Requests) response is received.add_bookmarkmethod to handletoo_many_requestserrors. This logic reads theRetry-Afterheader from Slack's response, pauses for the specified duration, and retries thebookmarks_addcall once. This mirrors the existing retry pattern fornot_in_channelerrors.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.addoperations 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