Skip to content

GH-3726: Avoid ConcurrentModificationException in KafkaMessageListenerContainer#3728

Merged
artembilan merged 1 commit into
spring-projects:mainfrom
tbarabanov:GH-3726
Jan 30, 2025
Merged

GH-3726: Avoid ConcurrentModificationException in KafkaMessageListenerContainer#3728
artembilan merged 1 commit into
spring-projects:mainfrom
tbarabanov:GH-3726

Conversation

@tbarabanov

Copy link
Copy Markdown
Contributor

Fixes possible ConcurrentModificationException in KafkaMessageListenerContainer that can happen in case if any of partitionsListenerConsumer.definedPartitions or partitionsListenerConsumer.assignedPartitions fields are being iterated/modified simultaneously.

For instance by consumer thread performing rebalancing and arbitrary application thread calling getAssignedPartitions method in KafkaMessageListenerContainer.

To achieve desired effect I'm using synchronized wrappers. I would prefer modern concurrent collections such as ConcurrentHashMap, but definedPartitions field has type of LinkedHashMap and its keySet preserves keys order.
Thus 1) no thread safe data structures available from std lib with same characteristics 2) possibly some users rely on the order. Anyway do not think that there are real life cases that can put definedPartitions or assignedPartitions under pressure.

Fixes GH-3726 (#3726)

@artembilan artembilan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please, add your name to the @author list of that changed class - and I'll merge it shortly.

Thank you!

…aMessageListenerContainer

Fixes spring-projectsGH-3726 (spring-projects#3726)

Signed-off-by: Tim Barabanov <t.barabanov@gmail.com>
@tbarabanov

Copy link
Copy Markdown
Contributor Author

Done. Thx!

@artembilan artembilan merged commit b772ebd into spring-projects:main Jan 30, 2025
@artembilan

Copy link
Copy Markdown
Member

Merged and back-ported to 3.2.x.

Thank you for contribution; looking forward for more!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KafkaMessageListenerContainer.getAssignedPartitions is not safe

2 participants