Skip to content

Commit 6ba72d9

Browse files
Merge pull request #879 from BLasan/test-branch-5.3.x
Fix: Socket Close Failure
2 parents 39cd887 + b07ded6 commit 6ba72d9

File tree

1 file changed

+12
-2
lines changed
  • components/data-bridge/org.wso2.carbon.databridge.receiver.binary/src/main/java/org/wso2/carbon/databridge/receiver/binary/internal

1 file changed

+12
-2
lines changed

components/data-bridge/org.wso2.carbon.databridge.receiver.binary/src/main/java/org/wso2/carbon/databridge/receiver/binary/internal/BinaryDataReceiver.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.io.*;
4343
import java.net.ServerSocket;
4444
import java.net.Socket;
45+
import java.net.SocketTimeoutException;
4546
import java.nio.ByteBuffer;
4647
import java.security.KeyManagementException;
4748
import java.security.KeyStore;
@@ -212,7 +213,6 @@ public void run() {
212213
while (true) {
213214
try {
214215
Socket socket = this.serverSocket.accept();
215-
socket.setSoTimeout(binaryDataReceiverConfiguration.getSocketTimeout());
216216
sslReceiverExecutorService.submit(new BinaryTransportReceiver(socket));
217217
} catch (IOException e) {
218218
log.error("Error while accepting the connection. ", e);
@@ -233,7 +233,6 @@ public void run() {
233233
while (true) {
234234
try {
235235
Socket socket = this.serverSocket.accept();
236-
socket.setSoTimeout(binaryDataReceiverConfiguration.getSocketTimeout());
237236
tcpReceiverExecutorService.submit(new BinaryTransportReceiver(socket));
238237
} catch (IOException e) {
239238
log.error("Error while accepting the connection. ", e);
@@ -252,6 +251,7 @@ public BinaryTransportReceiver(Socket socket) {
252251
@Override
253252
public void run() {
254253
try {
254+
this.socket.setSoTimeout(binaryDataReceiverConfiguration.getSocketTimeout());
255255
InputStream inputstream = new BufferedInputStream(socket.getInputStream());
256256
OutputStream outputStream = new BufferedOutputStream((socket.getOutputStream()));
257257
int messageType = inputstream.read();
@@ -261,8 +261,18 @@ public void run() {
261261
processMessage(messageType, message, outputStream);
262262
messageType = inputstream.read();
263263
}
264+
} catch (SocketTimeoutException socketTimeoutException) {
265+
log.error("Socket read timed out for client", socketTimeoutException);
264266
} catch (IOException ex) {
265267
log.error("Error while reading from the socket. ", ex);
268+
} finally {
269+
if (socket != null && !socket.isClosed()) {
270+
try {
271+
socket.close();
272+
} catch (IOException ex) {
273+
log.error("Error while closing socket", ex);
274+
}
275+
}
266276
}
267277
}
268278
}

0 commit comments

Comments
 (0)