Repository navigation
fix(sqs): confirm standard queue drain with queue attributes before exit_on_empty - #142
rahulpatra-pattern wants to merge 2 commits into
Conversation
…xit_on_empty An empty ReceiveMessage on a standard queue does not mean the queue is drained: messages held in flight by other consumers or left by failed acks reappear after the visibility timeout with no reader. Apply the GetQueueAttributes drain check to standard queues, ignoring receipts held by this process, and keep FIFO behaviour unchanged. Add unit tests with a fake client and document exit_on_empty semantics.
There was a problem hiding this comment.
🟡 Changes recommended
Critical SQS receipt-accounting flaws can falsely declare the queue drained and stop polling while failed acknowledgements or other consumers’ messages remain in flight.
3 open issues
What changed in this PR
Extends the SQS reader’s FIFO drain check from #123 to standard queues, reducing premature exit_on_empty shutdowns while messages remain invisible or delayed.
Changes:
- Checks queue attributes for standard queues, subtracting locally held receipts while preserving FIFO rules.
- Adds a warning and legacy-exit fallback for standard-queue permission errors.
- Introduces a mockable client interface, drain-check tests, and documentation of polling and IAM requirements.
| File | Description |
|---|---|
internal/pkg/pipeline/task/sqs/sqs.go |
Implements standard-queue drain checks and permission-error handling. |
internal/pkg/pipeline/task/sqs/sqs_test.go |
Tests drain decisions, receipt counts, API errors, and cancellation. |
internal/pkg/pipeline/task/sqs/README.md |
Documents exit behavior, receipt exemptions, and required permissions. |
Review effort: Balanced
| } | ||
| if s.outstanding.Load() > 0 { | ||
|
|
||
| held := int64(s.outstanding.Load()) |
| } | ||
| if s.outstanding.Load() > 0 { | ||
|
|
||
| held := int64(s.outstanding.Load()) |
| // not a reason to keep polling on a standard queue, where a downstream task | ||
| // (e.g. an unflushed join) may hold them until the output channel closes. | ||
| if !fifo { | ||
| depth.inFlight -= held |
|
/argus review |
| // not a reason to keep polling on a standard queue, where a downstream task | ||
| // (e.g. an unflushed join) may hold them until the output channel closes. | ||
| if !fifo { | ||
| depth.inFlight -= held |
There was a problem hiding this comment.
Concurrent buffering readers on one standard queue wait on each other until the visibility timeout
Severity: Medium
held only covers this process's receipts. Say two processes read the same standard queue with exit_on_empty: true, and both feed a fan-in task that holds acks until its input closes (the join case this subtraction exists for). Then each process counts the other's buffered receipts as in_flight_elsewhere. Neither reader can exit, and neither can release its receipts until it exits. This lasts until the visibility timeout expires. The messages then become visible again and get redelivered, so they are processed twice, and the stale receipt handles from the first delivery can no longer delete them. Before this PR, both readers exited on the empty receive. Parallel workers on one queue are a common Airflow pattern, so this is a likely configuration rather than an edge case.
Fix: State this limitation in the README "Exit on empty" section, and tell users to put an end_after shorter than the visibility timeout on fan-in pipelines that run more than one reader process. You could also add an opt-out (for example exit_on_empty_strict: false) that skips the "elsewhere" check for those pipelines.
There was a problem hiding this comment.
Argus review of fbbacac: I posted 4 inline findings (3 Medium, 1 Low).
The core change does what the PR description says. Standard queues now call GetQueueAttributes before exiting on an empty receive, and FIFO behaviour is unchanged. The new unit tests pass locally (go test ./internal/pkg/pipeline/task/sqs/...).
The findings cover:
- Concurrent buffering readers on one queue wait on each other until the visibility timeout.
- The not-drained path busy-loops when
wait_time_secondsis 0. - The README does not say a redrive policy is needed to avoid an endless reader.
- Two of the three AccessDenied codes have no tests.
Copilot already flagged the races where held is stale or over-counted, so I did not repeat them.
…ccessDenied codes; document redrive policy Addresses Argus review on #142: the not-drained path spun on ReceiveMessage and GetQueueAttributes when wait_time_seconds is 0; the README did not say a redrive policy is needed to bound a always-failing message; only one of the three AccessDenied codes was tested.
|
Pushed
Still open, by design for now:
Build, vet, gofmt and the |

Description
Make
exit_on_emptyon the SQS reader confirm the queue is drained for standard queues too, instead of exiting on the first emptyReceiveMessage.1. Problem
With
exit_on_empty: true, the reader on a standard queue exits successfully on the first empty receive. Messages that another worker, or an earlier failed attempt, received but did not delete are invisible until the visibility timeout ends (for example 1800 s). If the reader polls while they are in flight, it sees an empty receive and exits green. The messages reappear about 30 minutes later with no reader.2. Root cause
In
internal/pkg/pipeline/task/sqs/sqs.go,shouldExitOnEmptyhad a shortcut:Only FIFO queues got a real drain check (no outstanding receipts held by this process, and
GetQueueAttributesshowing visible, in-flight and delayed counts all 0, added in #123).messageAck.Ack(failed=true)deliberately leaves the receipt alone so SQS redelivers after the visibility timeout. Those messages are therefore invisible to the reader's empty receive by design.3. Incident evidence
Pattern data-acquisition, Amazon PDP
daily_v1DAG, 23 Nov 2026 (DATA-11609,amazon_pdp_daily_v1, data-airflow PR #8237). After worker failures the US queue was left with 84 messages in the queue plus 150 in its DLQ after the recovery run, even though the reader exited green. The downstream Airflowqueue_drainedcheck then failed the run.4. Fix and behaviour change
GetQueueAttributesand exits only ifApproximateNumberOfMessages,ApproximateNumberOfMessagesNotVisibleandApproximateNumberOfMessagesDelayedare all 0. Otherwise it logs and keeps polling.in_flight - outstanding). Without this, a pipeline whose downstream task holds acks until its input closes (for example the unflushed tail of ajoin, as intest/pipelines/sqs_fanout_fanin_dag.yaml, which uses a standard queue) would never exit, because the reader would wait on acks that can only settle after the reader stops. For FIFO the existing rule (outstanding > 0 blocks) is kept as is.end_afterto bound a run.Ack(failed=true)) are no longer counted as held once the ack settles, so they also keep the reader polling until the visibility timeout expires and they are redelivered. They then move toward the DLQ via the queue'smaxReceiveCountredrive policy rather than being silently left behind.sqs:GetQueueAttributesis now required for standard queues too. Before this change it was only called for FIFO queues. It was already listed under AWS Requirements in the README, but existing standard-queue pipelines may not have it granted. Roles must be checked before rollout.GetQueueAttributeserrors:end_afterormax_recordsstop it). That is kept for FIFO and for non-permission errors (throttling, network), since a transient error should not cause a false drain.GetQueueAttributes. To avoid turning a missing IAM grant into a hung run, anAccessDeniederror (codesAccessDenied,AccessDeniedException,AWS.SimpleQueueService.AccessDenied) on a standard queue logs a WARN and falls back to the previous behaviour of exiting on the empty receive. This is no worse than before, but it does not give the drain guarantee. Reviewers who prefer fail-fast (return an error) can say so; it is a one-line change.clientis now a minimalsqsClientinterface (ReceiveMessage, DeleteMessage, SendMessage, GetQueueAttributes) satisfied by*qs.Client.fifoQueueConfirmedEmpty/fifoDrainAttributeswere generalised toqueueDepth/queueDrainAttributes. A missing or non-numeric attribute is now an error (logged, keeps polling); previously it was a silent "not drained". Both keep polling.exit_on_emptysemantics (new "Exit on empty" section) and the IAM requirement.5. Tests
New
internal/pkg/pipeline/task/sqs/sqs_test.gowith a fake client (table test, 18 cases plus a cancelled-context test):GetQueueAttributeserror, missing or non-numeric attribute: no exitAccessDenied: exit with warning (legacy fallback)exit_on_empty: false: never exits and does not call the APIAccessDeniedkeep polling (unchanged)Run:
Not run: an integration run against LocalStack or real SQS.
6. Rollout and risk notes
exit_on_empty: truehassqs:GetQueueAttributeson that queue. Without it the run still exits (with a WARN in the logs) but without the drain guarantee.end_afteron those pipelines.ApproximateNumber*attributes are eventually consistent, so the check reduces rather than eliminates the race. A queue can briefly read 0 right after messages are sent.exit_on_empty: trueare the LocalStack fixturestest/pipelines/sqs_with_context_concurrency.yamlandtest/pipelines/sqs_fanout_fanin_dag.yaml(both standard queues), plus a docs example in the top-level README. Production pipelines live outside this repo.exit_on_empty: false(default; the reader then never exits on an empty receive, as before), or bound the run withend_after. FIFO behaviour is unchanged, so no action is needed there. Reverting this PR restores the old standard-queue behaviour.Types of changes
Checklist