Skip to content

fix(sqs): renew each predefined queue's STS credentials when they expire - #2900

Open
xKrishnaSaxena wants to merge 1 commit into
celery:mainfrom
xKrishnaSaxena:fix/sqs-sts-per-queue-expiry
Open

xKrishnaSaxena wants to merge 1 commit into
celery:mainfrom
xKrishnaSaxena:fix/sqs-sts-per-queue-expiry

Conversation

@xKrishnaSaxena

Copy link
Copy Markdown
Contributor

Note: Before submitting this pull request, please review our contributing
guidelines
& [Anti AI spamming Guidelines] (celery/celery#10932). Before opening an AI based PR make sure you asked
in the discussion forum or in the related open issues first.

Description

Fixes #2899

With sts_role_arn and two or more predefined queues, a worker stopped when the first STS credentials expired, with Unrecoverable error: Exception('Request HTTP Error HTTP 403 Forbidden (... ExpiredTokenException ...)') (the error in #2031).

Cause. Each predefined queue has its own boto3 client, made with the STS credentials fetched for that queue. The channel kept one sts_expiration for all of them, set by the client created last. When the credentials expire, the first queue to be used gets a new client and moves sts_expiration forward. Every other queue then passes is_sts_token_refresh_required() and keeps its client, whose credentials have expired. The poll for that queue (asynsqs() uses sqs(queue)) gets 403 ExpiredToken, and the error stops the worker. SnsFanout kept the same shared sts_expiration for predefined exchanges.

Change.

  • Channel keeps _sts_expirations (queue name => expiry of the credentials its client was made with), and _handle_sts_session() checks the queue's own expiry.
  • SnsFanout does the same per exchange.
  • sts_expiration is still set as before. Nothing uses it to decide on a refresh any more, but asynsqs() checks that it exists, and it is a public attribute. Happy to drop it if you prefer.
  • No new kind of AWS call: each queue already made its own AssumeRole call when first used. Now each queue renews when its own credentials expire, not only the first one.

Another option was one set of STS credentials shared by all queues, with every client rebuilt on refresh. That needs the credentials stored on the channel, so it is a larger change. This PR keeps the current one-AssumeRole-per-queue model.

Tests.

  • test_SQS.py::test_sts_session_expired_with_multiple_predefined_queues: two queues get clients, time moves past the expiry, queue-1 is refreshed, and queue-2 must then come back with unexpired credentials. Each fake client is the credentials it was made with, so the test checks the credentials themselves, not a call count.
  • test_SQS_SNS.py::test_sts_session_expired_with_multiple_predefined_exchanges: the same for SNS exchanges.
  • Existing STS tests that set sts_expiration to drive the refresh now set the per-queue / per-exchange expiry. test_sts_session_not_expired called sqs(), which test_Channel replaces with a mock, so it never reached this code. It now calls the real method, like the tests next to it.

Both new tests fail on main and pass with the fix.

End to end: a Celery 5.7 worker (solo pool) on two predefined queues with sts_role_arn, against moto behind a small proxy that answers 403 ExpiredToken when the request's access key has expired. The fake STS credentials last 15 s, and one task goes to each queue every 5 s:

main:   at 15 s queue-1 gets a new key, queue-2's next ReceiveMessage uses its old one -> 403 -> "Unrecoverable error", worker exits
branch: both queues get a new key every 15 s; 26 of 26 tasks run; no 403

Commands:

flake8 -j2 kombu t; pydocstyle kombu; mypy --config-file setup.cfg   pass (Python 3.13, pkgutils.txt pins)
pre-commit run --files <changed files>                               all hooks pass
python -bb -m pytest -rxs -xv --cov=kombu ... (3.12-unit command)    2495 passed, 170 skipped
pytest -xv -E sqs t/integration -n auto --reruns 2 --reruns-delay 1  goaws (as CI): 46 passed, 0 reruns
                                                                     moto: 44 passed, 2 failed*

* test_ack_invalid_receipt_with_backoff[...] fails on main under moto too and passes on goaws (moto, not this change).

As the template asks, I first raised this in #2899. I used an AI assistant for this change.

With sts_role_arn and more than one predefined queue, each queue gets its
own boto3 client, made with the STS credentials fetched for it. But the
channel kept a single sts_expiration, overwritten by whichever client was
created last. When the credentials expired, the first queue to be used
got a new client and moved sts_expiration forward, so every other queue
kept its client with expired credentials. Its requests got 403
ExpiredToken, and on the polling path that error stops a Celery worker
("Unrecoverable error").

The expiry is now kept per queue, and each client is renewed when its
own credentials expire. SNS fanout had the same shared expiry for
predefined exchanges and gets the same change. sts_expiration is still
set as before.
@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.47%. Comparing base (ffccd1c) to head (80eda1a).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2900   +/-   ##
=======================================
  Coverage   86.46%   86.47%           
=======================================
  Files          80       80           
  Lines       11860    11864    +4     
  Branches     1444     1444           
=======================================
+ Hits        10255    10259    +4     
  Misses       1390     1390           
  Partials      215      215           

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

@auvipy
auvipy self-requested a review November 30, 2026 17:33
@auvipy auvipy added this to the 5.7.x milestone Nov 30, 2026

@atishay-kasliwal atishay-kasliwal 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.

The two new tests with several predefined queues / exchanges fail on master: after queue-1 refreshes its credentials, queue-2 keeps its expired ones. All 14 STS tests in t/unit/transport/SQS/ pass here. asynsqs still checks hasattr(self, "sts_expiration"), which this branch keeps setting, so that path is unchanged.

@auvipy

auvipy commented Dec 1, 2026

Copy link
Copy Markdown
Member

The two new tests with several predefined queues / exchanges fail on master: after queue-1 refreshes its credentials, queue-2 keeps its expired ones. All 14 STS tests in t/unit/transport/SQS/ pass here. asynsqs still checks hasattr(self, "sts_expiration"), which this branch keeps setting, so that path is unchanged.

can you please make your comments more simplistic and clear? I found your comments or suggestions bit unclear to understand...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQS: with sts_role_arn and several predefined queues, all queues but one keep expired credentials after the first STS refresh

3 participants