From da7a3cb976d275d74632d7cddd266b1262a260c1 Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Sun, 11 Jun 2023 15:19:37 +0100 Subject: [PATCH] Fix concurrent collection modification issue in NetMarshalClient. --- .../lib/calmnet/marshal/NetMarshalClient.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/com/captainalm/lib/calmnet/marshal/NetMarshalClient.java b/src/com/captainalm/lib/calmnet/marshal/NetMarshalClient.java index e4b3450..dbe9284 100644 --- a/src/com/captainalm/lib/calmnet/marshal/NetMarshalClient.java +++ b/src/com/captainalm/lib/calmnet/marshal/NetMarshalClient.java @@ -18,6 +18,7 @@ import java.net.InetAddress; import java.net.MulticastSocket; import java.net.Socket; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.Queue; @@ -100,20 +101,27 @@ public class NetMarshalClient implements Closeable { fragmentSMM = new HashMap<>(); fragmentMonitorThread = new Thread(() -> { int ageCheckTime = this.fragmentationOptions.maximumFragmentAge - 1; + ArrayList idsToRemove = new ArrayList<>(); while (running) { synchronized (this.fragmentationOptions) { for (int c : fragmentRMM.keySet()) { - if (!fragmentRMM.get(c).plusSeconds(ageCheckTime).isAfter(LocalDateTime.now())) { - fragmentRMM.remove(c); - fragmentReceiver.deletePacketFromRegistry(c); - } + if (!fragmentRMM.get(c).plusSeconds(ageCheckTime).isAfter(LocalDateTime.now())) + idsToRemove.add(c); } + for (int c : idsToRemove) { + fragmentRMM.remove(c); + fragmentReceiver.deletePacketFromRegistry(c); + } + idsToRemove.clear(); for (int c : fragmentSMM.keySet()) { - if (!fragmentSMM.get(c).plusSeconds(ageCheckTime).isAfter(LocalDateTime.now())) { - fragmentSMM.remove(c); - fragmentSender.deletePacketFromRegistry(c); - } + if (!fragmentSMM.get(c).plusSeconds(ageCheckTime).isAfter(LocalDateTime.now())) + idsToRemove.add(c); } + for (int c : idsToRemove) { + fragmentSMM.remove(c); + fragmentSender.deletePacketFromRegistry(c); + } + idsToRemove.clear(); } try { Thread.sleep(this.fragmentationOptions.maximumFragmentAge);