Skip to content

fix: return the last response when async retries run out - #2088

Merged
WilliamBergamin merged 3 commits into
mainfrom
fix/async-retry-exhausted-response
Nov 26, 2026
Merged

WilliamBergamin merged 3 commits into
mainfrom
fix/async-retry-exhausted-response

Conversation

@WilliamBergamin

Copy link
Copy Markdown
Contributor

Summary

This pull request makes the async web, webhook, SCIM, and audit logs clients return the last response when their retries run out, as the sync clients already do.

Follow-up to #2085 (comment) (review thought on raise last_error).

  • Cause: the async loops only kept the response they were about to return, so after 100 response-driven retries (say a handler that keeps retrying 429s) last_error was still None and raise last_error raised TypeError: exceptions must derive from BaseException.
  • Each loop now keeps the last response. Web raises SlackApiError with the 429, the other three return the 429 response, matching sync.
  • This brings back a post-loop return in the spot refactor(web): remove unreachable resp checks from the async request loop #2085 removed as unreachable; with this change it is reachable.
  • SCIM and audit logs build the response lazily (functools.partial), since their response classes parse the body. Building it on every retry would change how a retried 429 with a malformed { body is handled.

Testing

  • New test_ratelimited_returns_the_last_response_when_retries_run_out in each async client's retry tests, with a no-sleep handler that retries 429s 100 times. Each fails on main with the TypeError and passes here.

Category

  • slack_sdk.web.WebClient (sync/async) (Web API client)
  • slack_sdk.webhook.WebhookClient (sync/async) (Incoming Webhook, response_url sender)
  • slack_sdk.socket_mode (Socket Mode client)
  • slack_sdk.signature (Request Signature Verifier)
  • slack_sdk.oauth (OAuth Flow Utilities)
  • slack_sdk.models (UI component builders)
  • slack_sdk.scim (SCIM API client)
  • slack_sdk.audit_logs (Audit Logs API client)
  • slack_sdk.rtm_v2 (RTM client)
  • /docs (Documents)
  • /tutorial (PythOnBoardingBot tutorial)
  • tests/integration_tests (Automated tests for this library)

Requirements

  • I've read and understood the Contributing Guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I have signed the Salesforce CLA (required before your PR can be reviewed or merged).
  • I've run python3 -m venv .venv && source .venv/bin/activate && ./scripts/run_validation.sh after making the changes.

🤖 Generated with Claude Code

After 100 response-driven retries the async web, webhook, SCIM, and audit
logs clients raised `None`, a TypeError, because they only kept the
response they were about to return. They now keep the last one, as the
sync clients do. SCIM and audit logs build it lazily since their response
classes parse the body.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
@WilliamBergamin WilliamBergamin added this to the 3.48.1 milestone Nov 26, 2026
@WilliamBergamin WilliamBergamin added bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented semver:patch web-client scim-client audit-logs-client area:async labels Nov 26, 2026
@WilliamBergamin WilliamBergamin self-assigned this Nov 26, 2026
@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.84%. Comparing base (61e25f5) to head (73459be).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2088      +/-   ##
==========================================
+ Coverage   84.79%   84.84%   +0.04%     
==========================================
  Files         118      118              
  Lines       14129    14136       +7     
==========================================
+ Hits        11981    11994      +13     
+ Misses       2148     2142       -6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@WilliamBergamin
WilliamBergamin marked this pull request as ready for review November 26, 2026 20:20
@WilliamBergamin
WilliamBergamin requested a review from a team as a code owner November 26, 2026 20:20
WilliamBergamin and others added 2 commits November 26, 2026 15:20
…tries

A retried 429 with a malformed `{` body now has a test in each client, so
building the response eagerly again would fail it: the handlers would retry
the parse error for the same attempt and send one request fewer.

Also drop the `resp` declaration and the `return resp` after `finally`,
which can't be reached now that `resp` is only set right before the
in-loop return.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>

@WilliamBergamin WilliamBergamin left a comment

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.

🚦 No breaking changes here: only the private retry helpers change, and the async clients now end their retries the way the sync clients already do. The new tests fail on main with the TypeError and pass here.
Left two optional comments, nothing blocking 💯

self.assertEqual([500], error_statuses(retry_handler.prepare_errors))

@async_test
async def test_ratelimited_returns_the_last_response_when_retries_run_out(self):

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.

🧪 suggestion (non-blocking): Could we add a retried 429 with a malformed { body for audit logs and SCIM? Nothing pins why the response is built lazily, so a later eager build would re-run the retry handlers with a JSONDecodeError and no test would catch it.

Comment thread slack_sdk/audit_logs/v1/async_client.py Resolved

@cchensh cchensh left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM. I confirmed the four new tests fail on main with the TypeError and pass here. A few non-blocking nits:

  • slack_sdk/webhook/async_client.py: the return resp after finally at the end of _perform_http_request is still unreachable. It was removed from SCIM and audit logs but not here.
  • A saved response now wins over a later exception: say attempt N gets a 429 and is retried, then later attempts raise (e.g. a disconnect) until the 100-attempt cap. We now return or raise the stale 429 instead of last_error. That matches the sync clients, so I'm fine keeping parity, but it might be worth a line in the description.
  • The description says the partial avoids changing how "a retried 429 with a malformed { body is handled". Main was already lazy, so the partial keeps the current async behavior. Building eagerly wouldn't abort the retry. The JSONDecodeError would be caught by except Exception and the handler asked again, so each attempt would use up two retries. The new malformed-body tests do pin this. The sync clients still raise on the first malformed 429 without retrying; that predates this PR.
  • Optional: the post-loop return in webhook, SCIM and audit logs skips _debug_log_response.

@WilliamBergamin
WilliamBergamin merged commit 8472703 into main Nov 26, 2026
19 checks passed
@WilliamBergamin
WilliamBergamin deleted the fix/async-retry-exhausted-response branch November 26, 2026 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:async audit-logs-client bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented scim-client semver:patch web-client

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants