Fix potential FetchResponse#responseData race condition issue
In Fix FetchResponse#responseData, we did a double-checked lock for the responseData, but the assignment of lazy-initialized object(responseData) didn't assign in the last step, which would let other threads get the partial object.
Reviewers: David Jacot Luke Chen
@@ -100,7 +100,10 @@ public class FetchResponse extends AbstractResponse {
if (responseData == null) {
synchronized (this) {
if (responseData == null) {
- responseData = new LinkedHashMap<>();
+ // Assigning the lazy-initialized responseData in the last step
+ // to avoid other threads accessing a half-initialized object.
+ final LinkedHashMap<TopicPartition, FetchResponseData.PartitionData> responseDataTmp =
+ new LinkedHashMap<>();
data.responses().forEach(topicResponse -> {
String name;
if (version < 13) {
@@ -110,9 +113,10 @@ public class FetchResponse extends AbstractResponse {
}
if (name != null) {
topicResponse.partitions().forEach(partition ->
- responseData.put(new TopicPartition(name, partition.partitionIndex()), partition));
+ responseDataTmp.put(new TopicPartition(name, partition.partitionIndex()), partition));
}
});
+ responseData = responseDataTmp;
}
}
}
