From 4d58ea51115940cf258a5d30d75de7a780a640d6 Mon Sep 17 00:00:00 2001 From: Miron Cuperman Date: Wed, 18 Jul 2012 10:48:07 -0700 Subject: [PATCH] Missing synchronization for iterations. (over synchronizedMap/synchronizedSet) --- .../com/google/bitcoin/core/PeerGroup.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java index febdf371..9003f0ef 100644 --- a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java +++ b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java @@ -304,7 +304,9 @@ public class PeerGroup { */ public synchronized List getConnectedPeers() { ArrayList result = new ArrayList(peers.size()); - result.addAll(peers); + synchronized (peers) { + result.addAll(peers); + } return result; } @@ -366,12 +368,14 @@ public class PeerGroup { FutureTask future = new FutureTask(new Runnable() { public void run() { // This is run with the peer group already locked. - for (Peer peer : peers) { - try { - log.info("{}: Sending transaction {}", peer.getAddress(), tx.getHashAsString()); - peer.sendMessage(tx); - } catch (IOException e) { - log.warn("Caught IOException whilst sending transaction: {}", e.getMessage()); + synchronized (peers) { + for (Peer peer : peers) { + try { + log.info("{}: Sending transaction {}", peer.getAddress(), tx.getHashAsString()); + peer.sendMessage(tx); + } catch (IOException e) { + log.warn("Caught IOException whilst sending transaction: {}", e.getMessage()); + } } } } @@ -433,9 +437,7 @@ public class PeerGroup { * {@link PeerEventListener} and use the onPeerConnected/onPeerDisconnected methods. */ public synchronized int numConnectedPeers() { - synchronized (peers) { - return peers.size(); - } + return peers.size(); } public synchronized boolean isRunning() { @@ -500,8 +502,10 @@ public class PeerGroup { synchronized (PeerGroup.this) { running = false; shutdownPeerDiscovery(); - for (ChannelFuture future : channelFutures.values()) { - future.getChannel().close(); + synchronized (channelFutures.values()) { + for (ChannelFuture future : channelFutures.values()) { + future.getChannel().close(); + } } bootstrap.releaseExternalResources(); }