Repository navigation
fix(sqs): reuse the FIFO deduplication id when a publish is retried - #2864
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 generated a new MessageDeduplicationId on every call for a FIFO queue. Connection.ensure retries a failed publish by calling _put again with the same message, so a SendMessage whose response was lost (SQS stored the message, the client saw a read timeout) went out again with a different id, and SQS stored the message twice. Record the generated id in the message properties so every retry of one publish sends the same id. _put_fanout had the same problem for FIFO SNS topics. A new publish still gets a new id: Producer.publish builds a new properties dict for each call, and so does Celery's Task.retry().
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2864 +/- ##
=======================================
Coverage 86.45% 86.46%
=======================================
Files 80 80
Lines 11853 11851 -2
Branches 1443 1442 -1
=======================================
- Hits 10248 10247 -1
+ Misses 1390 1389 -1
Partials 215 215 ☔ View full report in Codecov by Harness. |
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
On a FIFO queue, SQS drops a message whose
MessageDeduplicationIdit has already seen in the last 5 minutes. That is what makes it safe for a producer to retry a send.Channel._putgenerated a newMessageDeduplicationIdwithuuid4()on every call. WhenSendMessagereaches SQS but the response is lost (aReadTimeoutError, which the SQS transport treats as a connection error),Connection.ensureretries the publish by calling_putagain with the same message. The retry goes out with a different id, so SQS stores the message twice.botocore's own retries are not affected, because they resend the identical request. Only kombu's retry creates a new id.
_put_fanouthad the same problem for FIFO SNS topics.Reproduction on main (4cadf8b), with a fake FIFO queue that stores the message on the first
SendMessageand then raisesReadTimeoutError:Reproduction script (no AWS account needed)
With this PR applied, both attempts send the same id and the queue stores 1 message.
Fix
The generated id is now stored in the message properties with
setdefault, so the first attempt records it and every retry of the same publish reuses it. As with a caller-supplied id, the generated one now also appears in the message's properties in the body.What does not change:
MessageDeduplicationIdis still used as is.Producer.publishbuilds a new properties dict for each call, and Celery'sTask.retry()republishes with a new one too (signature_from_requestdoes not copy the received message's properties)._put_fanout, a message without apropertiesdict has nothing to store the id in, so it behaves as before.This touches the same function as #2863. I will rebase whichever of the two merges second.
Tests
test_fifo_put_reuses_deduplication_id_when_the_message_is_sent_again:_putcalled twice with the same message sends the same id and stores it in the properties.test_fifo_deduplication_id_survives_a_retried_publish:Producer.publish(retry=True)withsend_messageraisingReadTimeoutErroronce; both attempts carry the same id.test_fifo_each_publish_gets_its_own_deduplication_id: two separate publishes still get different ids.test_put_fanout_fifo_reuses_deduplication_id_when_sent_again: the same for_put_fanout.The first, second and fourth fail on main and pass with the fix; the third passes on both and guards against making the id global.
t/unit/transport/SQSandt/unit/asynchronouspass (549 tests), and flake8, isort and codespell are clean on the changed files.