fix: stop a malformed SQS body from poisoning the whole batch - #5215
Merged
edersonbrilhante merged 1 commit intoJul 21, 2026
Merged
Conversation
JSON.parse runs at the top of scaleUpHandler, outside the try/catch further down, so an unparseable message body threw straight out of the handler. Lambda treats a thrown exception as a complete batch failure, so nothing is deleted and the entire batch returns to the queue after the visibility timeout — and because the body is malformed deterministically, every redelivery reproduces it. scaleUp is never reached, so valid messages batched alongside it are never processed either. ReportBatchItemFailures also disables Lambda's scale-down of polling on failed invocations, so the usual backoff cushion does not apply here. Parse each record defensively and report only the malformed message as a batch item failure. The rest of the batch proceeds; the malformed message exhausts maxReceiveCount on its own. It cannot be acknowledged and discarded through that mechanism, so with the default redrive_build_queue disabled it expires by retention (24h default) rather than moving to a DLQ.
edersonbrilhante
approved these changes
Jul 21, 2026
edersonbrilhante
left a comment
Contributor
There was a problem hiding this comment.
Thanks for this PR
Copilot AI
pushed a commit
that referenced
this pull request
Jul 22, 2026
### Problem `JSON.parse(body)` runs in the record loop at the top of `scaleUpHandler`, outside the `try`/`catch` further down. An unparseable message body therefore throws straight out of the handler. Lambda treats a thrown exception as a complete batch failure, so **nothing is deleted and the entire batch returns to the queue** after the visibility timeout. The body is malformed deterministically, so every redelivery reproduces the same `SyntaxError`. `scaleUp` is never reached, which means valid messages batched alongside the malformed one are never processed either — they are blocked as collateral damage. Two things make it worse: - `ReportBatchItemFailures` [disables Lambda's scale-down of message polling when invocations fail](https://docs.aws.amazon.com/lambda/latest/dg/services-sqs-errorhandling.html), so the usual concurrency backoff does not apply. - `redrive_build_queue` defaults to `enabled = false`, so there is no `maxReceiveCount` cap. The only backstop is `job_queue_retention_in_seconds`, default 24h. Verified empirically against `main`: a batch of two valid records plus one malformed one rejects with `SyntaxError` and `scaleUp` is called zero times. ### Change Parse each record defensively. A malformed body is logged and reported as an individual batch item failure; the rest of the batch proceeds to `scaleUp` as normal. The malformed message then exhausts `maxReceiveCount` on its own rather than taking the batch with it. It cannot be acknowledged and discarded through the partial-failure mechanism — reporting it is the only way to single it out — so with the default configuration it expires by retention rather than moving to a DLQ. Operators who want these captured should enable `redrive_build_queue`. ### Tests Four tests covering: the invocation no longer fails, only the malformed message is reported, valid messages still reach `scaleUp`, and malformed and rejected messages combine correctly. All four fail against the current implementation. ### Note on scope This is distinct from #5129. That PR addresses the `catch` block, where a non-`ScaleError` returns an empty `batchItemFailures` — which AWS treats as complete success, so the batch is **deleted**. Opposite failure mode, different path, and worth keeping separate. I originally conflated the two in a comment on #5129 and have corrected it there. Touches the same file as #5129, so whichever lands second will need a trivial rebase.
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
JSON.parse(body)runs in the record loop at the top ofscaleUpHandler, outside thetry/catchfurther down. An unparseable message body therefore throws straight out of the handler.Lambda treats a thrown exception as a complete batch failure, so nothing is deleted and the entire batch returns to the queue after the visibility timeout. The body is malformed deterministically, so every redelivery reproduces the same
SyntaxError.scaleUpis never reached, which means valid messages batched alongside the malformed one are never processed either — they are blocked as collateral damage.Two things make it worse:
ReportBatchItemFailuresdisables Lambda's scale-down of message polling when invocations fail, so the usual concurrency backoff does not apply.redrive_build_queuedefaults toenabled = false, so there is nomaxReceiveCountcap. The only backstop isjob_queue_retention_in_seconds, default 24h.Verified empirically against
main: a batch of two valid records plus one malformed one rejects withSyntaxErrorandscaleUpis called zero times.Change
Parse each record defensively. A malformed body is logged and reported as an individual batch item failure; the rest of the batch proceeds to
scaleUpas normal.The malformed message then exhausts
maxReceiveCounton its own rather than taking the batch with it. It cannot be acknowledged and discarded through the partial-failure mechanism — reporting it is the only way to single it out — so with the default configuration it expires by retention rather than moving to a DLQ. Operators who want these captured should enableredrive_build_queue.Tests
Four tests covering: the invocation no longer fails, only the malformed message is reported, valid messages still reach
scaleUp, and malformed and rejected messages combine correctly. All four fail against the current implementation.Note on scope
This is distinct from #5129. That PR addresses the
catchblock, where a non-ScaleErrorreturns an emptybatchItemFailures— which AWS treats as complete success, so the batch is deleted. Opposite failure mode, different path, and worth keeping separate. I originally conflated the two in a comment on #5129 and have corrected it there.Touches the same file as #5129, so whichever lands second will need a trivial rebase.