Skip to content

KafkaMessageListenerContainer.getAssignedPartitions is not safe #3726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tbarabanov opened this issue Jan 29, 2025 · 1 comment · Fixed by #3728
Closed

KafkaMessageListenerContainer.getAssignedPartitions is not safe #3726

tbarabanov opened this issue Jan 29, 2025 · 1 comment · Fixed by #3728

Comments

@tbarabanov
Copy link
Contributor

In what version(s) of Spring for Apache Kafka are you seeing this issue?

3.2.1

Describe the bug

KafkaMessageListenerContainer.getAssignedPartitions() is not safe due to the fact that different threads can iterate/modify any of the fields partitionsListenerConsumer.definedPartitions, partitionsListenerConsumer.assignedPartitions simultaneously, but collection types of these fields are not designed for such scenarios. Thus at least ConcurrentModificationException can be thrown.

Expected behavior

KafkaMessageListenerContainer.getAssignedPartitions() method is public and thus expected to be safe.

@artembilan
Copy link
Member

tbarabanov added a commit to tbarabanov/spring-kafka that referenced this issue Jan 30, 2025
tbarabanov added a commit to tbarabanov/spring-kafka that referenced this issue Jan 30, 2025
tbarabanov added a commit to tbarabanov/spring-kafka that referenced this issue Jan 30, 2025
artembilan pushed a commit that referenced this issue Jan 30, 2025
…Exception

Fixes: #3726
Issue link: #3726

`KafkaMessageListenerContainer.getAssignedPartitions()` is not safe due to the fact that 
different threads can iterate/modify any of the fields `partitionsListenerConsumer.definedPartitions` or `partitionsListenerConsumer.assignedPartitions` simultaneously, 
but collection types of these fields are not designed for such scenarios. 
Thus at least `ConcurrentModificationException` can be thrown.

* Wrap `partitionsListenerConsumer.definedPartitions` and `partitionsListenerConsumer.assignedPartitions`  into `Collections.synchronizedSet()`

Signed-off-by: Tim Barabanov <[email protected]>

[[email protected] Fix commit message]

**Auto-cherry-pick to `3.2.x`**

Signed-off-by: Artem Bilan <[email protected]>
artembilan pushed a commit that referenced this issue Jan 30, 2025
…Exception

Fixes: #3726
Issue link: #3726

`KafkaMessageListenerContainer.getAssignedPartitions()` is not safe due to the fact that
different threads can iterate/modify any of the fields `partitionsListenerConsumer.definedPartitions` or `partitionsListenerConsumer.assignedPartitions` simultaneously,
but collection types of these fields are not designed for such scenarios.
Thus at least `ConcurrentModificationException` can be thrown.

* Wrap `partitionsListenerConsumer.definedPartitions` and `partitionsListenerConsumer.assignedPartitions`  into `Collections.synchronizedSet()`

Signed-off-by: Tim Barabanov <[email protected]>

[[email protected] Fix commit message]

# Conflicts:
#	spring-kafka/src/main/java/org/springframework/kafka/listener/KafkaMessageListenerContainer.java

Signed-off-by: Artem Bilan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants