Repository navigation
fix(sqs): keep message attributes when a publish is retried - #2863
Open
alirezaazadi wants to merge 2 commits into
Open
alirezaazadi wants to merge 2 commits into
alirezaazadi wants to merge 2 commits into
Conversation
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.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2863 +/- ##
==========================================
+ Coverage 86.46% 86.47% +0.01%
==========================================
Files 80 80
Lines 11860 11862 +2
Branches 1444 1444
==========================================
+ Hits 10255 10258 +3
+ Misses 1390 1389 -1
Partials 215 215 ☔ View full report in Codecov by Harness. |
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Channel._putusedmessage['properties'].pop('message_attributes')to keep the attributes out of the message body. That mutates the caller'spropertiesdict, which is shared:Producer.publishpasses the samepropertiesdict to every attempt thatConnection.ensuremakes. WhenSendMessagefails with a connection error (for example botocore'sEndpointConnectionError) and the publish is retried, the retry is sent withoutMessageAttributes.DirectExchange.delivercalls_putonce per queue), only the first queue gets the attributes.No error is raised, so the message is silently delivered without its attributes.
Reproduction on main (4cadf8b), with
send_messagefailing once and kombu retrying the publish:Reproduction script (no AWS account needed)
The SQS client is replaced by a fake whose first
SendMessageraisesEndpointConnectionError, so kombu's ownConnection.ensureretries the publish.With this PR applied, both attempts print the attributes.
Fix
Read
message_attributeswithout mutating the message, and serialize the body from a shallow copy whose properties omit them.The module docstring said to pass
message_attributestobasic_publish, but_putignoresbasic_publishkeyword arguments: the value is read from the properties passed toProducer.publish(or to Celery'sapply_async). The docstring now says so and shows an example.Tests
test_put_keeps_message_attributes_when_the_message_is_sent_again: calling_puttwice with the same message sends the attributes both times, leaves the message unchanged, and keeps the attributes out of the body.test_put_keeps_message_attributes_on_a_retried_publish:Producer.publish(retry=True)withsend_messageraisingEndpointConnectionErroronce; both attempts carry the attributes.Both tests fail on main and pass with the fix.
t/unit/transport/SQSandt/unit/asynchronouspass (547 tests), and flake8, isort and codespell are clean on the changed files.I did not find an existing issue for this.