Skip to content

fix(sqs): prevent ConcurrentModificationException on container stop#1632

Open
Baqirrizvidev wants to merge 2 commits into
awspring:mainfrom
Baqirrizvidev:fix/sqs-container-stop-concurrency
Open

fix(sqs): prevent ConcurrentModificationException on container stop#1632
Baqirrizvidev wants to merge 2 commits into
awspring:mainfrom
Baqirrizvidev:fix/sqs-container-stop-concurrency

Conversation

@Baqirrizvidev
Copy link
Copy Markdown

When an SQS message listener container is stopped under heavy load, AbstractPollingMessageSource.stop() iterates over this.pollingFutures to cancel active polls. Each future has a whenComplete callback registered in managePollingFuture that removes itself from the pollingFutures list.

Cancelling the future inside the iteration loop triggers the callback synchronously/concurrently, modifying the list while it is being iterated, which throws a ConcurrentModificationException and halts proper container shutdown.

This commit resolves the issue by taking a snapshot copy (new ArrayList<>(this.pollingFutures)) of the collection before iterating and cancelling, decoupling iteration from list modifications.

Fixes #1594

- Replace obsolete .collect(Collectors.toList()) with standard Java 16+ .toList()
- Remove unused java.util.stream.Collectors import
When an SQS message listener container is stopped, AbstractPollingMessageSource.stop() iterates over this.pollingFutures to cancel active polls. Each future has a whenComplete callback registered in managePollingFuture that synchronously/concurrently removes itself from the pollingFutures collection. Iterating over the collection while elements are being removed throws a ConcurrentModificationException.

This commit prevents the exception by taking a shallow snapshot copy of the pollingFutures collection before iterating and cancelling the futures.

Fixes awspring#1594
@github-actions github-actions Bot added the component: sqs SQS integration related issue label May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: sqs SQS integration related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue: Concurrent ModificationException when running multiple containers and restarting one of them

1 participant