sqs-consumer icon indicating copy to clipboard operation
sqs-consumer copied to clipboard

Heartbeat can fire after the handler settles, overriding terminateVisibilityTimeout and racing delete

Open goingforstudying-ctrl opened this issue 2 months ago • 2 comments

I run consumers with heartbeatInterval and terminateVisibilityTimeout: true so failed messages come back for retry quickly, and the timing of that doesn't quite work. Reading through processMessage, the heartbeat interval only gets cleared in the finally at the very end of the method. The catch block is still inside that window: it awaits changeVisibilityTimeout(message, 0) while the heartbeat is live. If a tick lands in there it sends another ChangeMessageVisibility with the full visibilityTimeout, which can land after the terminate call and the message stays hidden for the whole timeout anyway. Kind of defeats the point of terminating it.

The success path has the same shape. The heartbeat is still running while deleteMessage is awaited, so a tick can fire a ChangeMessageVisibility at a receipt handle that is mid-delete. When it lands after the delete, SQS errors and the consumer emits an "error" event for a message that was processed fine. Took me a while to work out where those occasional error events were coming from.

processMessageBatch has the same pattern on both paths.

I put together a repro with fake timers: handler settles at t=15s, the terminate call (or the delete) takes until t=20s, heartbeat tick at t=20s fires another extend. On current main the tick sends, on a patched consumer it doesn't.

Not sure there's a way to do better than a guard flag, since a tick whose request is already in flight can't be cancelled, but that shrinks the window to the unavoidable minimum. I'll send a PR in a bit with that approach plus regression tests for both paths, single and batch.

goingforstudying-ctrl avatar Sep 24 '26 14:09 goingforstudying-ctrl

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Oct 25 '26 01:10 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Oct 31 '26 01:10 github-actions[bot]