#27 socket.accept() block indefinately, preventing while loop from

exiting, preventing serversocket from ever reaching finally
This commit is contained in:
posdorfer 2017-11-23 16:20:24 +01:00
parent c0346319e0
commit daf8f0b6df

View File

@ -28,6 +28,7 @@ import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.HashSet; import java.util.HashSet;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -78,20 +79,26 @@ public class TSocket extends ASocket {
continueRunning = true; continueRunning = true;
int socketcount = 0; int socketcount = 0;
try (ServerSocket serverSocket = new ServerSocket(portNumber)) { try (ServerSocket serverSocket = new ServerSocket(portNumber)) {
serverSocket.setSoTimeout(1000);
while (continueRunning) { while (continueRunning) {
Socket clientSocket = serverSocket.accept(); try {
lastConnectedSocketTime = System.currentTimeMillis(); Socket clientSocket = serverSocket.accept();
String socketName = socketNameForCount(++socketcount); lastConnectedSocketTime = System.currentTimeMillis();
TSOCKET_LOG.debug("starting socket with: {}", socketName); String socketName = socketNameForCount(++socketcount);
SocketHandler t = (socketName != null) ? new SocketHandler(clientSocket, socketName) : new SocketHandler(clientSocket); TSOCKET_LOG.debug("starting socket with: {}", socketName);
t.start(); SocketHandler t = (socketName != null) ? new SocketHandler(clientSocket, socketName) : new SocketHandler(clientSocket);
runningThreads.add(t); t.start();
TSOCKET_LOG.debug("Started thread for sockethandling..."); runningThreads.add(t);
TSOCKET_LOG.debug("Started thread for sockethandling...");
} catch (SocketTimeoutException ste) {
// this is triggered by accept()
}
} }
TSOCKET_LOG.debug("TSocket Stopped Running"); TSOCKET_LOG.debug("TSocket Stopped Running");
} catch (IOException e) { } catch (IOException e) {
TSOCKET_LOG.error("Exception caught when trying to listen on port " + portNumber + " or listening for a connection", e); TSOCKET_LOG.error("Exception caught when trying to listen on port " + portNumber + " or listening for a connection", e);
} }
TSOCKET_LOG.debug("Exited main-run-while loop");
} }
private String socketNameForCount(int c) { private String socketNameForCount(int c) {
@ -124,6 +131,7 @@ public class TSocket extends ASocket {
runningThreads.clear(); runningThreads.clear();
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
TSOCKET_LOG.debug("Finished calling stop on members.");
} }
/** /**
* @return the amount of connected sockets, this should usually be 3: info,mempool and consensus * @return the amount of connected sockets, this should usually be 3: info,mempool and consensus