fix SNS PublishBatch modifying batch context for all subscribers after filtering #7674
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
After reading the report from #7662, I could not understand how this could happen when we were filtering the messages before passing the context to worker to send the message. If after filtering there were no messages to send, we would not pass the task to the executor, so no empty message entries could be passed to SQS.
However, after reading the publishing code again for
PublishBatch
, it showed that I was setting the filtered messages (messages are filtered on a subscriber basis) on the global context (used by all subscribers). It created 2 issues:messages
attributes while using the given context, which lead to the exception in the issue report.Solution
If some messages are filtered, we will create a copy of the context, set the filtered messages only on the copy and pass the copy to the worker, instead of always passing a global context. If no filter is set, we can safely pass the global one (allows us to not always copy the context if there are no reasons for it, workers do not manipulate the context, it is used read-only).
I've added a test case for the first issue, the second one will mostly happen as a race condition and is not always reproducible. But if one is fixed, so is there other.
fixes #7662