Fix concurrent collection modification issue in NetMarshalClient.

This commit is contained in:
Captain ALM 2023-06-11 15:19:37 +01:00
parent 6edf85ce51
commit da7a3cb976
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1

View File

@ -18,6 +18,7 @@ import java.net.InetAddress;
import java.net.MulticastSocket; import java.net.MulticastSocket;
import java.net.Socket; import java.net.Socket;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
@ -100,20 +101,27 @@ public class NetMarshalClient implements Closeable {
fragmentSMM = new HashMap<>(); fragmentSMM = new HashMap<>();
fragmentMonitorThread = new Thread(() -> { fragmentMonitorThread = new Thread(() -> {
int ageCheckTime = this.fragmentationOptions.maximumFragmentAge - 1; int ageCheckTime = this.fragmentationOptions.maximumFragmentAge - 1;
ArrayList<Integer> idsToRemove = new ArrayList<>();
while (running) { while (running) {
synchronized (this.fragmentationOptions) { synchronized (this.fragmentationOptions) {
for (int c : fragmentRMM.keySet()) { for (int c : fragmentRMM.keySet()) {
if (!fragmentRMM.get(c).plusSeconds(ageCheckTime).isAfter(LocalDateTime.now())) { if (!fragmentRMM.get(c).plusSeconds(ageCheckTime).isAfter(LocalDateTime.now()))
fragmentRMM.remove(c); idsToRemove.add(c);
fragmentReceiver.deletePacketFromRegistry(c);
}
} }
for (int c : idsToRemove) {
fragmentRMM.remove(c);
fragmentReceiver.deletePacketFromRegistry(c);
}
idsToRemove.clear();
for (int c : fragmentSMM.keySet()) { for (int c : fragmentSMM.keySet()) {
if (!fragmentSMM.get(c).plusSeconds(ageCheckTime).isAfter(LocalDateTime.now())) { if (!fragmentSMM.get(c).plusSeconds(ageCheckTime).isAfter(LocalDateTime.now()))
fragmentSMM.remove(c); idsToRemove.add(c);
fragmentSender.deletePacketFromRegistry(c);
}
} }
for (int c : idsToRemove) {
fragmentSMM.remove(c);
fragmentSender.deletePacketFromRegistry(c);
}
idsToRemove.clear();
} }
try { try {
Thread.sleep(this.fragmentationOptions.maximumFragmentAge); Thread.sleep(this.fragmentationOptions.maximumFragmentAge);