codeant-ai-for-open-source[bot] commented on code in PR #39914:
URL: https://github.com/apache/superset/pull/39914#discussion_r3476587426
##########
superset/reports/notifications/slackv2.py:
##########
@@ -77,7 +77,18 @@ def _get_inline_files(
return ("pdf", [self._content.pdf])
return (None, [])
- @backoff.on_exception(backoff.expo, SlackApiError, factor=10, base=2,
max_tries=5)
+ # Retry on NotificationUnprocessableException (the wrapper that send()
+ # raises for transient Slack failures: SlackApiError, connection errors,
+ # and the SlackClientError catch-all). Retrying on SlackApiError directly
+ # would never fire because the try/except below converts it before the
+ # decorator can see it. Mirrors the pattern in webhook.py.
+ @backoff.on_exception(
+ backoff.expo,
+ NotificationUnprocessableException,
+ factor=10,
+ base=2,
+ max_tries=5,
+ )
@statsd_gauge("reports.slack.send")
def send(self) -> None:
Review Comment:
**Suggestion:** Retrying the entire `send` method on
`NotificationUnprocessableException` can duplicate already-sent Slack
messages/files when a failure happens mid-loop (for example after some channels
succeeded and a later channel fails). On retry, the method replays all prior
channels/files from the beginning, causing duplicate notifications. Move retry
handling to the individual Slack API call level (or add idempotency guards) so
only failed sends are retried. [logic error]
<details>
<summary><b>Severity Level:</b> Major ⚠️</summary>
```mdx
- ❌ Slack alerts duplicate messages across channels on retry.
- ⚠️ Multi-channel Slack reports spam recipients after transient failures.
```
</details>
<details>
<summary><b>Steps of Reproduction ✅ </b></summary>
```mdx
1. In `superset/commands/report/execute.py:13-38`, `BaseReportState._send()`
builds a
notification via `create_notification(recipient, notification_content)`
(line 25), which
returns a `SlackV2Notification` instance when `recipient.type ==
ReportRecipientType.SLACKV2` (factory in
`superset/reports/notifications/__init__.py:25-34`).
2. `_send()` then calls `notification.send()` for non-dry runs at
`superset/commands/report/execute.py:36-37`, placing
`SlackV2Notification.send()` on the
main execution path for scheduled alerts/reports.
3. Inside `SlackV2Notification.send()`
(`superset/reports/notifications/slackv2.py:92-120`), `_get_channels()`
(lines 59-67)
parses a multi-channel target string like `"C12345,C67890,C11111"` (as used
in
`test_v2_send_to_multiple_channels_uploads_per_channel` at
`tests/unit_tests/reports/notifications/slack_tests.py:86-102`) into a list,
then `send()`
iterates `for channel in channels` and, when files are present, nested `for
file in files`
calls `client.files_upload_v2(...)` for each channel/file pair.
4. If a transient Slack failure occurs after some uploads/messages
succeed—for example,
`client.files_upload_v2` or `client.chat_postMessage` raises `SlackApiError`
for a later
channel/file—`SlackV2Notification.send()` maps that to
`NotificationUnprocessableException` via the `except
(SlackClientNotConnectedError,
SlackApiError)` and `except SlackClientError` blocks at
`superset/reports/notifications/slackv2.py:128-141`, re-raising
`NotificationUnprocessableException` after earlier channels/files have
already been sent.
5. The `@backoff.on_exception(NotificationUnprocessableException, ...)`
decorator on
`send()` at `superset/reports/notifications/slackv2.py:80-91` catches this
exception and
retries the entire `send()` method from the beginning, re-running the `for
channel` / `for
file` loops and re-invoking `files_upload_v2`/`chat_postMessage` for
channels/files that
previously succeeded, thereby duplicating Slack messages/files for those
recipients on
each retry attempt.
```
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=e552b10b71b44cc28f76f417201ae726&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=e552b10b71b44cc28f76f417201ae726&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset/reports/notifications/slackv2.py
**Line:** 85:93
**Comment:**
*Logic Error: Retrying the entire `send` method on
`NotificationUnprocessableException` can duplicate already-sent Slack
messages/files when a failure happens mid-loop (for example after some channels
succeeded and a later channel fails). On retry, the method replays all prior
channels/files from the beginning, causing duplicate notifications. Move retry
handling to the individual Slack API call level (or add idempotency guards) so
only failed sends are retried.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39914&comment_hash=ecbeaa9dd5e86181eab5d2c09ac86f2d1ef9c90b1bd7e1ac104db51b2bf29992&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39914&comment_hash=ecbeaa9dd5e86181eab5d2c09ac86f2d1ef9c90b1bd7e1ac104db51b2bf29992&reaction=dislike'>👎</a>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]