From 86b26dedf14d98fc74d36cd9597afe37c321303f Mon Sep 17 00:00:00 2001 From: Alireza Azadi Date: Thu, 19 Nov 2026 23:23:24 +0100 Subject: [PATCH] fix(sqs): keep message attributes when a publish is retried Channel._put popped message_attributes from the message's properties dict. Producer.publish passes that same dict to every attempt that Connection.ensure makes, so a SendMessage retried after a connection error went out without its MessageAttributes, and so did a message delivered to more than one bound queue. Read the attributes without mutating the message and build the body from a copy that omits them. Also fix the module docstring: message_attributes is read from the properties passed to Producer.publish, not from basic_publish kwargs. --- kombu/transport/SQS/__init__.py | 22 ++++++++++---- t/unit/transport/SQS/test_SQS.py | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/kombu/transport/SQS/__init__.py b/kombu/transport/SQS/__init__.py index 302fcd6fc1..972e9720be 100644 --- a/kombu/transport/SQS/__init__.py +++ b/kombu/transport/SQS/__init__.py @@ -89,8 +89,15 @@ https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-message-metadata.html SQS supports sending message attributes along with the message body. -To use this feature, you can pass a 'message_attributes' as keyword argument -to `basic_publish` method. +To use this feature, pass ``message_attributes`` to ``Producer.publish`` +(or to Celery's ``apply_async``); it is sent as the SQS ``MessageAttributes`` +and kept out of the message body: + +.. code-block:: python + + producer.publish(body, routing_key='myqueue', message_attributes={ + 'tenant': {'DataType': 'String', 'StringValue': '42'}, + }) Other Features supported by this transport ========================================== @@ -646,10 +653,13 @@ def _put(self, queue, message, **kwargs): q_url = self._new_queue(queue) kwargs = {'QueueUrl': q_url} if 'properties' in message: - if 'message_attributes' in message['properties']: - # we don't want to want to have the attribute in the body - kwargs['MessageAttributes'] = \ - message['properties'].pop('message_attributes') + properties = message['properties'] + if 'message_attributes' in properties: + kwargs['MessageAttributes'] = properties['message_attributes'] + message = {**message, 'properties': { + key: value for key, value in properties.items() + if key != 'message_attributes' + }} # Support SQS fair queue system. if 'MessageGroupId' in message['properties']: kwargs['MessageGroupId'] = \ diff --git a/t/unit/transport/SQS/test_SQS.py b/t/unit/transport/SQS/test_SQS.py index f018976af1..8c75f8ccd9 100644 --- a/t/unit/transport/SQS/test_SQS.py +++ b/t/unit/transport/SQS/test_SQS.py @@ -7,6 +7,7 @@ from __future__ import annotations import base64 +import json import logging import os import random @@ -2149,6 +2150,55 @@ def test_fifo_queue_with_custom_message_group_id_and_deduplication_id(self): assert call_kwargs['MessageGroupId'] == 'tenant-456' assert call_kwargs['MessageDeduplicationId'] == 'unique-msg-789' + def test_put_keeps_message_attributes_when_the_message_is_sent_again(self): + connection = Connection(transport=SQS.Transport, transport_options={ + 'predefined_queues': example_predefined_queues, + }) + channel = connection.channel() + channel.sqs = Mock() + send_message = channel.sqs.return_value.send_message + attributes = {'tenant': {'DataType': 'String', 'StringValue': '42'}} + message = channel.prepare_message( + 'body', properties={'message_attributes': attributes}, + ) + + channel._put('queue-1', message) + channel._put('queue-1', message) + + assert [ + sent.kwargs['MessageAttributes'] + for sent in send_message.call_args_list + ] == [attributes, attributes] + assert message['properties']['message_attributes'] == attributes + body = json.loads( + base64.b64decode(send_message.call_args.kwargs['MessageBody']) + ) + assert 'message_attributes' not in body['properties'] + + def test_put_keeps_message_attributes_on_a_retried_publish(self): + connection = Connection(transport=SQS.Transport, transport_options={ + 'predefined_queues': example_predefined_queues, + }) + sqs_client = Mock() + sqs_client.send_message.side_effect = [ + EndpointConnectionError(endpoint_url='https://sqs.us-east-1'), + {'MessageId': '1'}, + ] + attributes = {'tenant': {'DataType': 'String', 'StringValue': '42'}} + + with patch.object(SQS.Channel, 'sqs', return_value=sqs_client): + messaging.Producer(connection).publish( + 'body', routing_key='queue-1', retry=True, + retry_policy={'max_retries': 1, 'interval_start': 0, + 'interval_step': 0, 'interval_max': 0}, + message_attributes=attributes, + ) + + assert [ + sent.kwargs['MessageAttributes'] + for sent in sqs_client.send_message.call_args_list + ] == [attributes, attributes] + def test_message_group_id_with_special_characters(self): """Test MessageGroupId with special characters""" connection = Connection(transport=SQS.Transport, transport_options={