Skip to content

GH-2974: Fix race in TcpNetConnection.getPayload() [51x] #2976

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

Merged
merged 2 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ private void trackComponentIfAny(TrackableComponent component) {
component.setShouldTrack(shouldTrack);
if (shouldTrack) {
this.currentlyTrackedComponents.add(component);
if (this.logger.isInfoEnabled()) {
this.logger.info("Enabling MessageHistory tracking for component '" + componentName + "'");
if (logger.isInfoEnabled()) {
logger.info("Enabling MessageHistory tracking for component '" + componentName + "'");
}
}
}
Expand Down Expand Up @@ -217,8 +217,8 @@ public void stop() {
if (this.running) {
this.currentlyTrackedComponents.forEach(component -> {
component.setShouldTrack(false);
if (this.logger.isInfoEnabled()) {
this.logger.info("Disabling MessageHistory tracking for component '"
if (logger.isInfoEnabled()) {
logger.info("Disabling MessageHistory tracking for component '"
+ component.getComponentName() + "'");
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,15 @@ public synchronized void send(Message<?> message) throws Exception {

@Override
public Object getPayload() throws Exception {
InputStream inputStream;
try {
inputStream = inputStream();
}
catch (IOException e1) {
throw new SoftEndOfStreamException("Socket closed when getting input stream", e1);
}
return getDeserializer()
.deserialize(inputStream());
.deserialize(inputStream);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,29 @@ public class SoftEndOfStreamException extends IOException {

private static final long serialVersionUID = 7309907445617226978L;

/**
* Default constructor.
*/
public SoftEndOfStreamException() {
super();
}

/**
* Construct an instance with the message.
* @param message the message.
*/
public SoftEndOfStreamException(String message) {
super(message);
}

/**
* Construct an instance with the message and cause.
* @param message the message.
* @param cause the cause.
* @since 4.3.21.
*/
public SoftEndOfStreamException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,39 @@

package org.springframework.integration.ip.tcp.connection;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import javax.net.SocketFactory;

import org.apache.commons.logging.Log;
import org.junit.Test;
import org.mockito.Mockito;

import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream;
import org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer;
import org.springframework.integration.ip.tcp.serializer.MapJsonSerializer;
import org.springframework.integration.ip.tcp.serializer.SoftEndOfStreamException;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.support.converter.MapMessageConverter;
import org.springframework.integration.test.util.TestUtils;
Expand Down Expand Up @@ -137,4 +147,30 @@ public void transferHeaders() throws Exception {
assertEquals("baz", inboundMessage.get().getHeaders().get("bar"));
}

@Test
public void socketClosedNextRead() throws InterruptedException, IOException {
TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory(0);
AtomicInteger port = new AtomicInteger();
CountDownLatch latch = new CountDownLatch(1);
ApplicationEventPublisher publisher = ev -> {
if (ev instanceof TcpConnectionServerListeningEvent) {
port.set(((TcpConnectionServerListeningEvent) ev).getPort());
latch.countDown();
}
};
server.setApplicationEventPublisher(publisher);
server.registerListener(message -> {
return false;
});
server.afterPropertiesSet();
server.start();
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
Socket socket = SocketFactory.getDefault().createSocket("localhost", port.get());
TcpNetConnection connection = new TcpNetConnection(socket, false, false, publisher, "socketClosedNextRead");
socket.close();
assertThatThrownBy(() -> connection.getPayload())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not going to back-port smoothly: there is no AssertJ in the requested versions.
I'll figure during cherry-picking...

.isInstanceOf(SoftEndOfStreamException.class);
server.stop();
}

}