Skip to content

Commit

Permalink
Fix server deadlock caused by lossy division operations
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed May 8, 2024
1 parent c0916bb commit c950dde
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/main/java/gripe/_90/appliede/me/service/EMCStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public long extract(AEKey what, long amount, Actionable mode, IActionSource sour
var providers = new ArrayList<IKnowledgeProvider>();

if (source.player().isPresent() && AppliedEConfig.CONFIG.terminalExtractFromOwnEmcOnly()) {
providers.add(
service.getProviderFor(source.player().get().getUUID()).get());
var provider = service.getProviderFor(source.player().get().getUUID());
providers.add(provider.get());
} else {
providers.addAll(service.getProviders());
}
Expand All @@ -117,6 +117,7 @@ public long extract(AEKey what, long amount, Actionable mode, IActionSource sour
var divisor = BigInteger.valueOf(service.getProviders().size());
var quotient = toExtract.divide(divisor);
var remainder = toExtract.remainder(divisor).longValue();
var lastExtracted = 0L;

for (var p = 0; p < providers.size(); p++) {
var provider = providers.get(p);
Expand All @@ -129,15 +130,21 @@ public long extract(AEKey what, long amount, Actionable mode, IActionSource sour
provider.setEmc(BigInteger.ZERO);
}

extracted += currentEmc.divide(multiplier).longValue();
lastExtracted = currentEmc.divide(multiplier).longValue();
extracted += lastExtracted;
// provider exhausted, remove from current list to re-extract deficit from remaining providers
providers.remove(provider);
} else {
if (mode == Actionable.MODULATE) {
provider.setEmc(currentEmc.subtract(toExtractFrom));
}

extracted += toExtractFrom.divide(multiplier).longValue();
lastExtracted = toExtractFrom.divide(multiplier).longValue();
extracted += lastExtracted;
}

if (lastExtracted == 0) {
providers.remove(provider);
}
}
}
Expand Down

0 comments on commit c950dde

Please sign in to comment.