potential concurracy error fix

This commit is contained in:
sirjonasxx 2018-10-16 01:17:52 +02:00
parent c620d84c2e
commit 5b11349133

View File

@ -148,15 +148,19 @@ public class Extensions extends SubForm {
getHConnection().addStateChangeListener((oldState, newState) -> { getHConnection().addStateChangeListener((oldState, newState) -> {
if (newState == HConnection.State.CONNECTED) { if (newState == HConnection.State.CONNECTED) {
synchronized (gEarthExtensions) {
for (GEarthExtension extension : gEarthExtensions) { for (GEarthExtension extension : gEarthExtensions) {
extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.CONNECTIONSTART)); extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.CONNECTIONSTART));
} }
} }
}
if (oldState == HConnection.State.CONNECTED) { if (oldState == HConnection.State.CONNECTED) {
synchronized (getHConnection()) {
for (GEarthExtension extension : gEarthExtensions) { for (GEarthExtension extension : gEarthExtensions) {
extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.CONNECTIONEND)); extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.CONNECTIONEND));
} }
} }
}
}); });
getHConnection().addTrafficListener(1, message -> { getHConnection().addTrafficListener(1, message -> {
@ -172,7 +176,17 @@ public class Extensions extends SubForm {
boolean[] isblock = new boolean[1]; boolean[] isblock = new boolean[1];
for (GEarthExtension extension : collection) { Iterator<GEarthExtension> it;
synchronized (collection) {
it = collection.iterator();
}
while (true) {
GEarthExtension extension;
synchronized (collection) {
if (!it.hasNext()) break;
extension = it.next();
}
GEarthExtension.ReceiveMessageListener respondCallback = new GEarthExtension.ReceiveMessageListener() { GEarthExtension.ReceiveMessageListener respondCallback = new GEarthExtension.ReceiveMessageListener() {
@Override @Override
public void act(HPacket packet) { public void act(HPacket packet) {
@ -196,22 +210,26 @@ public class Extensions extends SubForm {
} }
}; };
extension.addOnReceiveMessageListener(respondCallback); extension.addOnReceiveMessageListener(respondCallback);
extension.sendMessage(manipulatePacketRequest); extension.sendMessage(manipulatePacketRequest);
} }
//block untill all extensions have responded //block untill all extensions have responded
List<GEarthExtension> willdelete = new ArrayList<>(); List<GEarthExtension> willdelete = new ArrayList<>();
while (!collection.isEmpty()) { while (true) {
synchronized (collection) { synchronized (collection) {
if (collection.isEmpty()) {
break;
}
for (GEarthExtension extension : collection) { for (GEarthExtension extension : collection) {
synchronized (gEarthExtensions) { synchronized (gEarthExtensions) {
if (!gEarthExtensions.contains(extension)) willdelete.add(extension); if (!gEarthExtensions.contains(extension)) willdelete.add(extension);
} }
} }
for (int i = willdelete.size() - 1; i >= 0; i--) { for (int i = willdelete.size() - 1; i >= 0; i--) {
synchronized (collection) {
collection.remove(willdelete.get(i)); collection.remove(willdelete.get(i));
}
willdelete.remove(i); willdelete.remove(i);
} }
} }
@ -259,7 +277,9 @@ public class Extensions extends SubForm {
} }
} }
}; };
synchronized (messageListeners) {
messageListeners.put(extension, receiveMessageListener); messageListeners.put(extension, receiveMessageListener);
}
extension.addOnReceiveMessageListener(receiveMessageListener); extension.addOnReceiveMessageListener(receiveMessageListener);
extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.INIT)); extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.INIT));
@ -283,8 +303,10 @@ public class Extensions extends SubForm {
gEarthExtensions.remove(extension); gEarthExtensions.remove(extension);
} }
synchronized (messageListeners) {
extension.removeOnReceiveMessageListener(messageListeners.get(extension)); extension.removeOnReceiveMessageListener(messageListeners.get(extension));
messageListeners.remove(extension); messageListeners.remove(extension);
}
Platform.runLater(extension::delete); Platform.runLater(extension::delete);
} }
}); });