Skip to content

Fix intermittent ConcurrentModificationException (#92) #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.mirth.connect.donkey.model.message.attachment.Attachment;
import com.mirth.connect.donkey.util.MapUtil;
import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("message")
Expand Down Expand Up @@ -152,8 +154,8 @@ public int compare(ConnectorMessage m1, ConnectorMessage m2) {
if (sourceMap == null) {
sourceMap = connectorMessage.getSourceMap();
}
responseMap.putAll(connectorMessage.getResponseMap());
channelMap.putAll(connectorMessage.getChannelMap());
MapUtil.safePutAll(responseMap, connectorMessage.getResponseMap(), 5);
MapUtil.safePutAll(channelMap, connectorMessage.getChannelMap(), 5);
}
}

Expand Down
34 changes: 34 additions & 0 deletions donkey/src/main/java/com/mirth/connect/donkey/util/MapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package com.mirth.connect.donkey.util;

import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -132,4 +133,37 @@ public static Map<String, Object> deserializeMapWithInvalidValues(Serializer ser

return map;
}

/**
* Tries to safely put all entries from a possibly concurrently modified source map into the
* target map. Retries up to the specified maxAttempts if ConcurrentModificationException is
* thrown, sleeping 10ms between retries.
*
* @param target
* The destination map
* @param source
* The source map that might be modified concurrently
* @param maxAttempts
* The maximum number of retries
* @return true if the putAll succeeded, false if it failed after retries
*/
public static <K, V> boolean safePutAll(Map<K, V> target, Map<K, V> source, final int maxAttempts) {

int attempts = 0;
while (attempts++ < maxAttempts) {
try {
target.putAll(source);
return true;
} catch (ConcurrentModificationException e) {
try {
Thread.sleep(10);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
return false;
}
}
}

return false;
}
}