Skip to content

Commit 3cccf9b

Browse files
artembilangaryrussell
authored andcommitted
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 3e99ae3 commit 3cccf9b

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.
@@ -35,6 +35,8 @@
3535
* determine success.
3636
*
3737
* @author Gary Russell
38+
* @author Artem Bilan
39+
*
3840
* @since 2.0
3941
*/
4042
public class MulticastSendingMessageHandler extends UnicastSendingMessageHandler {
@@ -101,49 +103,45 @@ public MulticastSendingMessageHandler(String address, int port,
101103

102104
@Override
103105
protected DatagramSocket getSocket() throws IOException {
104-
if (this.getTheSocket() == null) {
106+
if (this.multicastSocket == null) {
105107
synchronized (this) {
106-
createSocket();
108+
if (this.multicastSocket == null) {
109+
createSocket();
110+
}
107111
}
108112
}
109-
return this.getTheSocket();
113+
return getTheSocket();
110114
}
111115

112116
private void createSocket() throws IOException {
113-
if (this.getTheSocket() == null) {
114-
MulticastSocket socket;
115-
if (this.isAcknowledge()) {
116-
int ackPort = this.getAckPort();
117-
if (this.localAddress == null) {
118-
socket = ackPort == 0 ? new MulticastSocket() : new MulticastSocket(ackPort);
119-
}
120-
else {
121-
InetAddress whichNic = InetAddress.getByName(this.localAddress);
122-
socket = new MulticastSocket(new InetSocketAddress(whichNic, ackPort));
123-
}
124-
if (getSoReceiveBufferSize() > 0) {
125-
socket.setReceiveBufferSize(this.getSoReceiveBufferSize());
126-
}
127-
if (logger.isDebugEnabled()) {
128-
logger.debug("Listening for acks on port: " + socket.getLocalPort());
129-
}
130-
setSocket(socket);
131-
updateAckAddress();
117+
MulticastSocket socket;
118+
if (isAcknowledge()) {
119+
int ackPort = getAckPort();
120+
if (this.localAddress == null) {
121+
socket = ackPort == 0 ? new MulticastSocket() : new MulticastSocket(ackPort);
132122
}
133123
else {
134-
socket = new MulticastSocket();
135-
setSocket(socket);
124+
InetAddress whichNic = InetAddress.getByName(this.localAddress);
125+
socket = new MulticastSocket(new InetSocketAddress(whichNic, ackPort));
136126
}
137-
if (this.timeToLive >= 0) {
138-
socket.setTimeToLive(this.timeToLive);
127+
if (getSoReceiveBufferSize() > 0) {
128+
socket.setReceiveBufferSize(getSoReceiveBufferSize());
139129
}
140-
setSocketAttributes(socket);
141-
if (this.localAddress != null) {
142-
InetAddress whichNic = InetAddress.getByName(this.localAddress);
143-
socket.setInterface(whichNic);
130+
if (logger.isDebugEnabled()) {
131+
logger.debug("Listening for acks on port: " + socket.getLocalPort());
144132
}
145-
this.multicastSocket = socket;
133+
setSocket(socket);
134+
updateAckAddress();
135+
}
136+
else {
137+
socket = new MulticastSocket();
138+
setSocket(socket);
139+
}
140+
if (this.timeToLive >= 0) {
141+
socket.setTimeToLive(this.timeToLive);
146142
}
143+
setSocketAttributes(socket);
144+
this.multicastSocket = socket;
147145
}
148146

149147

@@ -153,7 +151,7 @@ private void createSocket() throws IOException {
153151
* @param minAcksForSuccess The minimum number of acks that will represent success.
154152
*/
155153
public void setMinAcksForSuccess(int minAcksForSuccess) {
156-
this.setAckCounter(minAcksForSuccess);
154+
setAckCounter(minAcksForSuccess);
157155
}
158156

159157
/**

0 commit comments

Comments
 (0)