diff --git a/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java b/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java index a14ca25514..534cac75b3 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java +++ b/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java @@ -24,7 +24,6 @@ import bisq.common.UserThread; import bisq.common.app.Capabilities; import bisq.common.proto.network.NetworkEnvelope; import bisq.common.proto.network.NetworkProtoResolver; -import bisq.common.util.SingleThreadExecutorUtils; import bisq.common.util.Utilities; import com.runjva.sourceforge.jsocks.protocol.Socks5Proxy; @@ -50,7 +49,6 @@ import java.util.HashSet; import java.util.Optional; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; -import java.util.concurrent.ExecutorService; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -81,7 +79,6 @@ public abstract class NetworkNode implements MessageListener { final CopyOnWriteArraySet setupListeners = new CopyOnWriteArraySet<>(); private final ListeningExecutorService connectionExecutor; private final ListeningExecutorService sendMessageExecutor; - private final ExecutorService serverExecutor; private Server server; private volatile boolean shutDownInProgress; @@ -112,7 +109,6 @@ public abstract class NetworkNode implements MessageListener { maxConnections * 3, 30, 30); - serverExecutor = SingleThreadExecutorUtils.getSingleThreadExecutor("NetworkNode.server-" + servicePort); } /////////////////////////////////////////////////////////////////////////////////////////// @@ -373,7 +369,6 @@ public abstract class NetworkNode implements MessageListener { if (server != null) { server.shutDown(); server = null; - serverExecutor.shutdownNow(); } Set allConnections = getAllConnections(); @@ -503,7 +498,7 @@ public abstract class NetworkNode implements MessageListener { connectionListener, networkProtoResolver, networkFilter); - serverExecutor.submit(server); + server.start(); } private Optional lookupOutBoundConnection(NodeAddress peersNodeAddress) { diff --git a/p2p/src/main/java/bisq/network/p2p/network/Server.java b/p2p/src/main/java/bisq/network/p2p/network/Server.java index 04a7f296a0..d08047c1f5 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/Server.java +++ b/p2p/src/main/java/bisq/network/p2p/network/Server.java @@ -42,9 +42,11 @@ class Server implements Runnable { private final NetworkFilter networkFilter; private final ServerSocket serverSocket; + private final int localPort; private final Set connections = new CopyOnWriteArraySet<>(); private volatile boolean stopped; private final NetworkProtoResolver networkProtoResolver; + private final Thread serverThread = new Thread(this); public Server(ServerSocket serverSocket, @@ -54,19 +56,23 @@ class Server implements Runnable { @Nullable NetworkFilter networkFilter) { this.networkProtoResolver = networkProtoResolver; this.serverSocket = serverSocket; + this.localPort = serverSocket.getLocalPort(); this.messageListener = messageListener; this.connectionListener = connectionListener; this.networkFilter = networkFilter; } + public void start() { + serverThread.setName("Server-" + localPort); + serverThread.start(); + } + @Override public void run() { try { - // Thread created by NetworkNode - Thread.currentThread().setName("Server-" + serverSocket.getLocalPort()); try { while (!stopped && !Thread.currentThread().isInterrupted()) { - log.debug("Ready to accept new clients on port " + serverSocket.getLocalPort()); + log.debug("Ready to accept new clients on port " + localPort); final Socket socket = serverSocket.accept(); if (!stopped && !Thread.currentThread().isInterrupted()) { log.debug("Accepted new client on localPort/port " + socket.getLocalPort() + "/" + socket.getPort()); @@ -106,8 +112,11 @@ class Server implements Runnable { connections.forEach(connection -> connection.shutDown(CloseConnectionReason.APP_SHUT_DOWN)); try { - if (!serverSocket.isClosed()) + if (!serverSocket.isClosed()) { serverSocket.close(); + } + serverThread.interrupt(); + } catch (SocketException e) { log.debug("SocketException at shutdown might be expected " + e.getMessage()); } catch (IOException e) {