Skip to content

Commit 806ffef

Browse files
committed
INT-4366: Fix MulticastSendingMessageHandler
JIRA: https://jira.spring.io/browse/INT-4366 Fix race condition in the `MulticastSendingMessageHandler` around `multicastSocket` and super `socket` properties. * Synchronize around `this` and check for the `multicastSocket == null`. This let the `MulticastSendingMessageHandler` to fully configure and prepare the socket for use. * Remove `socket.setInterface(whichNic)` since it is populated by the `InetSocketAddress` ctor before **Cherry-pick to 4.3.x**
1 parent e6ec86c commit 806ffef

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

spring-integration-ip/src/main/java/org/springframework/integration/ip/udp/MulticastSendingMessageHandler.java

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2001-2016 the original author or authors.
2+
* Copyright 2001-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,6 +38,8 @@
3838
* determine success.
3939
*
4040
* @author Gary Russell
41+
* @author Artem Bilan
42+
*
4143
* @since 2.0
4244
*/
4345
public class MulticastSendingMessageHandler extends UnicastSendingMessageHandler {
@@ -126,49 +128,45 @@ public MulticastSendingMessageHandler(String destinationExpression) {
126128

127129
@Override
128130
protected DatagramSocket getSocket() throws IOException {
129-
if (this.getTheSocket() == null) {
131+
if (this.multicastSocket == null) {
130132
synchronized (this) {
131-
createSocket();
133+
if (this.multicastSocket == null) {
134+
createSocket();
135+
}
132136
}
133137
}
134-
return this.getTheSocket();
138+
return getTheSocket();
135139
}
136140

137141
private void createSocket() throws IOException {
138-
if (this.getTheSocket() == null) {
139-
MulticastSocket socket;
140-
if (this.isAcknowledge()) {
141-
int ackPort = this.getAckPort();
142-
if (this.localAddress == null) {
143-
socket = ackPort == 0 ? new MulticastSocket() : new MulticastSocket(ackPort);
144-
}
145-
else {
146-
InetAddress whichNic = InetAddress.getByName(this.localAddress);
147-
socket = new MulticastSocket(new InetSocketAddress(whichNic, ackPort));
148-
}
149-
if (getSoReceiveBufferSize() > 0) {
150-
socket.setReceiveBufferSize(this.getSoReceiveBufferSize());
151-
}
152-
if (logger.isDebugEnabled()) {
153-
logger.debug("Listening for acks on port: " + socket.getLocalPort());
154-
}
155-
setSocket(socket);
156-
updateAckAddress();
142+
MulticastSocket socket;
143+
if (isAcknowledge()) {
144+
int ackPort = getAckPort();
145+
if (this.localAddress == null) {
146+
socket = ackPort == 0 ? new MulticastSocket() : new MulticastSocket(ackPort);
157147
}
158148
else {
159-
socket = new MulticastSocket();
160-
setSocket(socket);
149+
InetAddress whichNic = InetAddress.getByName(this.localAddress);
150+
socket = new MulticastSocket(new InetSocketAddress(whichNic, ackPort));
161151
}
162-
if (this.timeToLive >= 0) {
163-
socket.setTimeToLive(this.timeToLive);
152+
if (getSoReceiveBufferSize() > 0) {
153+
socket.setReceiveBufferSize(getSoReceiveBufferSize());
164154
}
165-
setSocketAttributes(socket);
166-
if (this.localAddress != null) {
167-
InetAddress whichNic = InetAddress.getByName(this.localAddress);
168-
socket.setInterface(whichNic);
155+
if (logger.isDebugEnabled()) {
156+
logger.debug("Listening for acks on port: " + socket.getLocalPort());
169157
}
170-
this.multicastSocket = socket;
158+
setSocket(socket);
159+
updateAckAddress();
160+
}
161+
else {
162+
socket = new MulticastSocket();
163+
setSocket(socket);
164+
}
165+
if (this.timeToLive >= 0) {
166+
socket.setTimeToLive(this.timeToLive);
171167
}
168+
setSocketAttributes(socket);
169+
this.multicastSocket = socket;
172170
}
173171

174172

@@ -178,7 +176,7 @@ private void createSocket() throws IOException {
178176
* @param minAcksForSuccess The minimum number of acks that will represent success.
179177
*/
180178
public void setMinAcksForSuccess(int minAcksForSuccess) {
181-
this.setAckCounter(minAcksForSuccess);
179+
setAckCounter(minAcksForSuccess);
182180
}
183181

184182
/**

0 commit comments

Comments
 (0)