Potential fix for issue #22

This commit is contained in:
catbref 2020-08-24 14:27:03 +01:00
parent 9e52f20f71
commit b4301f125d

View File

@ -96,22 +96,24 @@ public class Network {
private final String ourNodeId = Crypto.toNodeAddress(edPublicKeyParams.getEncoded()); private final String ourNodeId = Crypto.toNodeAddress(edPublicKeyParams.getEncoded());
private final int maxMessageSize; private final int maxMessageSize;
private final int minOutboundPeers;
private final int maxPeers;
private final List<PeerData> allKnownPeers = new ArrayList<>(); private final List<PeerData> allKnownPeers = new ArrayList<>();
private final List<Peer> connectedPeers = new ArrayList<>(); private final List<Peer> connectedPeers = new ArrayList<>();
private final List<PeerAddress> selfPeers = new ArrayList<>(); private final List<PeerAddress> selfPeers = new ArrayList<>();
private ExecuteProduceConsume networkEPC; private final ExecuteProduceConsume networkEPC;
private Selector channelSelector; private Selector channelSelector;
private ServerSocketChannel serverChannel; private ServerSocketChannel serverChannel;
private Iterator<SelectionKey> channelIterator = null; private Iterator<SelectionKey> channelIterator = null;
private int minOutboundPeers; // volatile because value is updated inside any one of the EPC threads
private int maxPeers; private volatile long nextConnectTaskTimestamp = 0L; // ms - try first connect once NTP syncs
private long nextConnectTaskTimestamp = 0L; // ms - try first connect once NTP syncs
private ExecutorService broadcastExecutor = Executors.newCachedThreadPool(); private ExecutorService broadcastExecutor = Executors.newCachedThreadPool();
private long nextBroadcastTimestamp = 0L; // ms - try first broadcast once NTP syncs // volatile because value is updated inside any one of the EPC threads
private volatile long nextBroadcastTimestamp = 0L; // ms - try first broadcast once NTP syncs
private final Lock mergePeersLock = new ReentrantLock(); private final Lock mergePeersLock = new ReentrantLock();
@ -429,6 +431,8 @@ public class Network {
private Task maybeProduceChannelTask(boolean canBlock) throws InterruptedException { private Task maybeProduceChannelTask(boolean canBlock) throws InterruptedException {
final SelectionKey nextSelectionKey; final SelectionKey nextSelectionKey;
// Synchronization here to enforce thread-safety on channelIterator
synchronized (channelSelector) {
// anything to do? // anything to do?
if (channelIterator == null) { if (channelIterator == null) {
try { try {
@ -457,6 +461,7 @@ public class Network {
LOGGER.trace(() -> String.format("Thread %d, nextSelectionKey %s, channelIterator now %s", LOGGER.trace(() -> String.format("Thread %d, nextSelectionKey %s, channelIterator now %s",
Thread.currentThread().getId(), nextSelectionKey, channelIterator)); Thread.currentThread().getId(), nextSelectionKey, channelIterator));
}
if (nextSelectionKey == null) if (nextSelectionKey == null)
return null; return null;