Skip to content

Add retries and timeouts to st2client HTTP requests - #6428

Open
Vipulucc115 wants to merge 2 commits into
StackStorm:masterfrom
Vipulucc115:feat/st2client-http-retries
Open

Vipulucc115 wants to merge 2 commits into
StackStorm:masterfrom
Vipulucc115:feat/st2client-http-retries

Conversation

@Vipulucc115

@Vipulucc115 Vipulucc115 commented Dec 5, 2026 •

Copy link
Copy Markdown

Closes part 1 of #3257 (retry logic for st2client).

Impact: st2 commands no longer hang forever when the API can't be reached, and no longer fail on a short blip (an API restart, a load balancer answering 502/503/504). A failed request is retried up to 3 times, and connecting times out after 10 seconds. A POST that may already have reached the server is never sent twice, so an action can't be run twice by a retry.

What changed

st2client/utils/httpclient.py

  • HTTPClient now sends its requests through a requests.Session with urllib3's Retry mounted on an HTTPAdapter, as suggested in the issue.
  • Retry rules (get_http_retry()):
    • Connection errors are retried for every method: the request never reached the server.
    • Read timeouts and 502 / 503 / 504 responses are retried only for idempotent methods (GET, PUT, DELETE, HEAD, OPTIONS), never POST or PATCH.
    • Other errors (400, 401, 404, 500, SSL errors) are not retried.
    • When retries run out on a bad status code, the last response is returned rather than raised, so handle_error() reports it exactly as before.
    • Retry-After is honoured but capped at 30s, so a proxy can't make the CLI sleep for an hour.
    • Backoff: the first retry is immediate, then 1s, 2s, ...
  • Default timeout (10, None): 10s to connect and, as today, no limit on waiting for the reply, so slow calls like pack installs aren't cut off.
  • The session blocks cookies, so requests stay independent like the module-level requests.get() calls they replace. The API's auth-token cookie isn't stored and resent.

Settings (client.py, models/core.py, base.py, config_parser.py, shell.py)

  • --http-timeout / --http-retries CLI options
  • ST2_HTTP_TIMEOUT / ST2_HTTP_RETRIES environment variables
  • http_timeout / http_retries in the [general] section of the CLI config (documented in conf/st2rc.sample.ini)
  • Client(timeout=..., retries=...) for library users. Both are passed to every resource manager and HTTPClient, the same way cacert is.
  • Invalid values (--http-retries -1, ST2_HTTP_TIMEOUT=abc, ...) fail with a clear error before any request is sent.
  • Precedence follows the existing options such as cacert: CLI argument > config file > environment variable > default.

Not in this PR

  • The event stream (StreamManager.listen, used by st2 execution tail) still calls requests.get(..., stream=True) directly. Retrying a long-lived stream needs different handling.
  • Part 2 of the issue (retrying git clones in st2 pack install) runs on the server side and would be a separate change.

Tests

  • New st2client/tests/unit/test_httpclient.py (25 tests). The retry tests run against a small HTTP server in a thread and count the requests it actually receives. They cover:
    • GET/PUT/DELETE recovering from 502/503/504;
    • POST/PATCH sent only once;
    • 400/401/404/500 not retried;
    • giving up and returning the last response;
    • retries=0;
    • connection errors retried for GET and POST;
    • a read timeout retried for GET but not for POST;
    • Retry-After honoured and capped;
    • cookies not sent back;
    • CLI / env / config / Client wiring and validation.
  • Existing tests that mocked requests.get/post/put/delete now mock the same methods on requests.Session and expect the default timeout. The full-config fixture and test_config_parser include the two new options.
  • pytest st2client/tests/unit: 310 passed, 1 skipped (Python 3.11). black and flake8 are clean.

Failed requests to the StackStorm API are retried (3 times by default) and connecting times out after 10s instead of hanging. Connection errors are retried for every method; read timeouts and 502/503/504 only for idempotent methods, so a POST is never sent twice. Configurable via --http-retries/--http-timeout, ST2_HTTP_RETRIES/ST2_HTTP_TIMEOUT or http_retries/http_timeout in the CLI config. Part of StackStorm#3257.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@pull-request-size pull-request-size Bot added the size/XL PR that changes 500-999 lines. Consider splitting work into several ones that easier to review. label Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL PR that changes 500-999 lines. Consider splitting work into several ones that easier to review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants