fix error on shut down broadcasting to peers

This commit is contained in:
woodser 2024-06-30 11:54:26 -04:00
parent 8ce91aa0c1
commit ec9f91e014
2 changed files with 13 additions and 5 deletions

View File

@ -38,6 +38,7 @@ import com.google.common.util.concurrent.SettableFuture;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import lombok.Getter;
import java.net.ServerSocket;
import java.net.Socket;
@ -82,7 +83,8 @@ public abstract class NetworkNode implements MessageListener {
private final ListeningExecutorService sendMessageExecutor;
private Server server;
private volatile boolean shutDownInProgress;
@Getter
private volatile boolean isShutDownStarted;
// accessed from different threads
private final CopyOnWriteArraySet<OutboundConnection> outBoundConnections = new CopyOnWriteArraySet<>();
protected final ObjectProperty<NodeAddress> nodeAddressProperty = new SimpleObjectProperty<>();
@ -181,7 +183,7 @@ public abstract class NetworkNode implements MessageListener {
try {
socket.close();
} catch (Throwable throwable) {
if (!shutDownInProgress) {
if (!isShutDownStarted) {
log.error("Error at closing socket " + throwable);
}
}
@ -362,8 +364,8 @@ public abstract class NetworkNode implements MessageListener {
public void shutDown(Runnable shutDownCompleteHandler) {
log.info("NetworkNode shutdown started");
if (!shutDownInProgress) {
shutDownInProgress = true;
if (!isShutDownStarted) {
isShutDownStarted = true;
if (server != null) {
server.shutDown();
server = null;

View File

@ -352,7 +352,13 @@ public class BroadcastHandler implements PeerManager.Listener {
sendMessageFutures.stream()
.filter(future -> !future.isCancelled() && !future.isDone())
.forEach(future -> future.cancel(true));
.forEach(future -> {
try {
future.cancel(true);
} catch (Exception e) {
if (!networkNode.isShutDownStarted()) throw e;
}
});
sendMessageFutures.clear();
peerManager.removeListener(this);