GH-4273: Fix ConcurrentModificationException in `getAssignedPartiti…#4274
Merged
Conversation
…AssignedPartitions()` Fixes spring-projects#4273 The previous fix (spring-projectsGH-3726) wrapped `definedPartitions` and `assignedPartitions` with `Collections.synchronizedMap/Set`, but this only synchronizes individual operations. Per Java documentation, manual synchronization is required when iterating over synchronized collection views. The method was returning unmodifiable views of these collections, which callers would then iterate outside any synchronization. When partitions were reassigned concurrently (e.g., during rebalance), the iteration would fail with `ConcurrentModificationException`. The fix synchronizes on the underlying collection while creating a snapshot copy. This ensures the iteration (during copy) is protected, and callers receive an independent snapshot safe to use without holding any locks. Additionally, `wrapUp()` now explicitly passes an empty collection to `onPartitionsRevoked()` during shutdown. Previously, this accidentally worked because the mutable view would become empty after `unsubscribe()` cleared the underlying collection. Since `getAssignedPartitions()` now returns a snapshot, we must explicitly pass an empty collection to preserve the shutdown signal semantics. The actual partition cleanup already occurs via the rebalance listener during `unsubscribe()`. Signed-off-by: Soby Chacko <soby.chacko@broadcom.com>
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.
…ons()`
Fixes #4273
The previous fix (GH-3726) wrapped
definedPartitionsandassignedPartitionswithCollections.synchronizedMap/Set, but this only synchronizes individual operations. Per Java documentation, manual synchronization is required when iterating over synchronized collection views.The method was returning unmodifiable views of these collections, which callers would then iterate outside any synchronization. When partitions were reassigned concurrently (e.g., during rebalance), the iteration would fail with
ConcurrentModificationException.The fix synchronizes on the underlying collection while creating a snapshot copy. This ensures the iteration (during copy) is protected, and callers receive an independent snapshot safe to use without holding any locks.
Additionally,
wrapUp()now explicitly passes an empty collection toonPartitionsRevoked()during shutdown. Previously, this accidentally worked because the mutable view would become empty afterunsubscribe()cleared the underlying collection. SincegetAssignedPartitions()now returns a snapshot, we must explicitly pass an empty collection to preserve the shutdown signal semantics. The actual partition cleanup already occurs via the rebalance listener duringunsubscribe().