Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix SNS PublishBatch modifying batch context for all subscribers after filtering #7674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2023

Conversation

bentsku
Copy link
Contributor

@bentsku bentsku commented Feb 13, 2023

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:

  • First, if in the order of subscription, we have a subscription with a filter then one without filter, we would filter the message and set them in the global context, so the next subscription in line would not have any messages in its context, even if it had no filter.
  • Second, if the order is reversed. A subscription with no filter, then one with a filter. We would in this order:
    • Iterate over subscribers
    • During iteration, send the context to the first worker with for example 1 message
    • Continuing iteration, filter the messages depending on the subscriber and set them in the global context
    • Sending it to the second worker
    • During that time, the first worker would start its job, and might have an empty 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

@bentsku bentsku requested a review from thrau February 13, 2023 14:58
@bentsku bentsku self-assigned this Feb 13, 2023
@bentsku bentsku temporarily deployed to localstack-ext-tests February 13, 2023 14:58 — with GitHub Actions Inactive
@bentsku bentsku added the aws:sns Amazon Simple Notification Service label Feb 13, 2023
@bentsku bentsku temporarily deployed to localstack-ext-tests February 13, 2023 15:58 — with GitHub Actions Inactive
@github-actions
Copy link

LocalStack integration with Pro

       3 files  ±0         3 suites  ±0   1h 40m 50s ⏱️ +24s
1 726 tests +1  1 368 ✔️ +3  358 💤  - 2  0 ±0 
2 440 runs  +1  1 742 ✔️ +3  698 💤  - 2  0 ±0 

Results for commit d2bb537. ± Comparison against base commit 204e605.

Copy link
Member

@thrau thrau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🥇 for the ever increasing test coverage :-)

@thrau thrau merged commit 8a3ffe8 into master Feb 14, 2023
@bentsku bentsku deleted the fix-sns-batch branch February 16, 2023 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aws:sns Amazon Simple Notification Service
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: Unable to forward SNS message to SQS: An error occurred (AWS.SimpleQueueService.EmptyBatchRequest) when calling the SendMessageBatch
2 participants