Skip to content

Commit

Permalink
Fix shutdown handling and concurrent modification exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Sep 21, 2024
1 parent 514bb2b commit 0ec7ff3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,14 @@ private void start() {
});
}

public CompletableFuture<?> destroyInstance() {
public CompletableFuture<?> deleteInstance() {
return stopAttackPermanently().thenRun(scheduler::shutdown);
}

public CompletableFuture<?> shutdownHook() {
return stopAttackSession().thenRun(scheduler::shutdown);
}

public CompletableFuture<?> stopAttackPermanently() {
if (attackLifecycle.isStoppedOrStopping()) {
return CompletableFuture.completedFuture(null);
Expand Down
24 changes: 12 additions & 12 deletions server/src/main/java/com/soulfiremc/server/SoulFireServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

@Slf4j
Expand All @@ -90,7 +91,7 @@ public class SoulFireServer implements EventBusOwner<SoulFireGlobalEvent> {
new InjectorBuilder().addDefaultHandlers("com.soulfiremc").create();
private final SoulFireScheduler scheduler = new SoulFireScheduler(log);
private final Map<String, String> serviceServerConfig = new HashMap<>();
private final Map<UUID, InstanceManager> instances = Collections.synchronizedMap(new HashMap<>());
private final Map<UUID, InstanceManager> instances = new ConcurrentHashMap<>();
private final RPCServer rpcServer;
private final ServerSettingsRegistry serverSettingsRegistry;
private final ServerSettingsRegistry instanceSettingsRegistry;
Expand Down Expand Up @@ -282,18 +283,18 @@ private String generateJWT(String subject) {
}

private void shutdownHook() {
// Shutdown the attacks if there is any
stopAllAttacksSessions().join();

// Shutdown scheduled tasks
scheduler.shutdown();

// Shut down RPC
try {
rpcServer.shutdown();
} catch (InterruptedException e) {
log.error("Failed to stop RPC server", e);
}

// Shutdown the attacks if there is any
shutdownInstances().join();

// Shutdown scheduled tasks
scheduler.shutdown();
}

public UUID createInstance(String friendlyName) {
Expand All @@ -307,15 +308,14 @@ public UUID createInstance(String friendlyName) {
return attackManager.id();
}

public CompletableFuture<?> stopAllAttacksSessions() {
return CompletableFuture.allOf(
Set.copyOf(instances.values()).stream()
.map(InstanceManager::stopAttackSession)
public CompletableFuture<?> shutdownInstances() {
return CompletableFuture.allOf(instances.values().stream()
.map(InstanceManager::shutdownHook)
.toArray(CompletableFuture[]::new));
}

public Optional<CompletableFuture<?>> deleteInstance(UUID id) {
return Optional.ofNullable(instances.remove(id)).map(InstanceManager::destroyInstance);
return Optional.ofNullable(instances.remove(id)).map(InstanceManager::deleteInstance);
}

public Optional<InstanceManager> getInstance(UUID id) {
Expand Down

0 comments on commit 0ec7ff3

Please sign in to comment.