Skip to content

Commit

Permalink
fixed ConcurrentModificationException when releasing service
Browse files Browse the repository at this point in the history
  • Loading branch information
supertick committed Dec 10, 2023
1 parent bd92b80 commit 796966a
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/main/java/org/myrobotlab/service/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ public class Runtime extends Service<RuntimeConfig> implements MessageListener,
// FIXME - AVOID STATIC FIELDS !!! use .getInstance() to get the singleton

/**
* a registry of all services regardless of which environment they came from -
* each must have a unique name
* A registry of all services regardless of which environment they came from -
* each must have a unique name.
*/
static private final Map<String, ServiceInterface> registry = new TreeMap<>();
static private Map<String, ServiceInterface> registry = new TreeMap<>();

/**
* A plan is a request to runtime to change the system. Typically its to ask
Expand Down Expand Up @@ -2005,8 +2005,16 @@ synchronized public static void unregister(String inName) {

// FIXME - release autostarted peers ?

// last step - remove from registry
registry.remove(name);
// last step - remove from registry by making new registry
// thread safe way
Map<String, ServiceInterface> removedService = new TreeMap<>();
for (String key: registry.keySet()) {
if (!name.equals(key)) {
removedService.put(key, registry.get(key));
}
}
registry = removedService;

// and config
RuntimeConfig c = (RuntimeConfig) Runtime.getInstance().config;
c.registry.remove(CodecUtils.getShortName(name));
Expand Down Expand Up @@ -2130,11 +2138,10 @@ static private void processRelease(boolean releaseRuntime) {
// clean up remote ... the contract should
// probably be just remove their references - do not
// ask for them to be released remotely ..
for (String remoteService : registry.keySet()) {
if (!remoteService.equals(runtime.getFullName())) {
registry.remove(remoteService);
}
}
// in thread safe way
Map<String, ServiceInterface> removedAllServices = new TreeMap<>();
removedAllServices.put(runtime.getFullName(), registry.get(runtime.getFullName()));
registry = removedAllServices;

if (runtime != null && releaseRuntime) {
runtime.releaseService();
Expand Down

0 comments on commit 796966a

Please sign in to comment.