3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 07:12:17 +00:00

Fix PeerGroup.peers iterator synchronization

This commit is contained in:
Miron Cuperman (devrandom) 2011-07-25 20:06:51 +00:00
parent 652a394fc1
commit 8469c8fd86

View File

@ -160,12 +160,14 @@ public class PeerGroup {
*/ */
public boolean broadcastTransaction(Transaction tx) { public boolean broadcastTransaction(Transaction tx) {
boolean success = false; boolean success = false;
for (Peer peer : peers) { synchronized (peers) {
try { for (Peer peer : peers) {
peer.broadcastTransaction(tx); try {
success = true; peer.broadcastTransaction(tx);
} catch (IOException e) { success = true;
log.error("failed to broadcast to " + peer, e); } catch (IOException e) {
log.error("failed to broadcast to " + peer, e);
}
} }
} }
return success; return success;
@ -196,8 +198,10 @@ public class PeerGroup {
peerPool.shutdownNow(); peerPool.shutdownNow();
for (Peer peer : peers) { synchronized (peers) {
peer.disconnect(); for (Peer peer : peers) {
peer.disconnect();
}
} }
} }
@ -268,8 +272,12 @@ public class PeerGroup {
// TODO be more nuanced about which peer to download from. We can also try // TODO be more nuanced about which peer to download from. We can also try
// downloading from multiple peers and handle the case when a new peer comes along // downloading from multiple peers and handle the case when a new peer comes along
// with a longer chain after we thought we were done. // with a longer chain after we thought we were done.
if (!peers.isEmpty()) synchronized (peers) {
startBlockChainDownloadFromPeer(peers.iterator().next()); if (!peers.isEmpty())
{
startBlockChainDownloadFromPeer(peers.iterator().next());
}
}
} }
/** /**
@ -296,8 +304,11 @@ public class PeerGroup {
protected synchronized void handlePeerDeath(Peer peer) { protected synchronized void handlePeerDeath(Peer peer) {
if (peer == downloadPeer) { if (peer == downloadPeer) {
downloadPeer = null; downloadPeer = null;
if (downloadListener != null && !peers.isEmpty()) synchronized (peers) {
startBlockChainDownloadFromPeer(peers.iterator().next()); if (downloadListener != null && !peers.isEmpty()) {
startBlockChainDownloadFromPeer(peers.iterator().next());
}
}
} }
} }