From c950dde12f2b96f9e148a189a6e57d6208a62bc9 Mon Sep 17 00:00:00 2001 From: 90 Date: Wed, 8 May 2024 21:44:53 +0100 Subject: [PATCH] Fix server deadlock caused by lossy division operations --- .../gripe/_90/appliede/me/service/EMCStorage.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/gripe/_90/appliede/me/service/EMCStorage.java b/src/main/java/gripe/_90/appliede/me/service/EMCStorage.java index b0e801b..6deb7fc 100644 --- a/src/main/java/gripe/_90/appliede/me/service/EMCStorage.java +++ b/src/main/java/gripe/_90/appliede/me/service/EMCStorage.java @@ -104,8 +104,8 @@ public long extract(AEKey what, long amount, Actionable mode, IActionSource sour var providers = new ArrayList(); 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()); } @@ -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); @@ -129,7 +130,8 @@ 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 { @@ -137,7 +139,12 @@ public long extract(AEKey what, long amount, Actionable mode, IActionSource sour provider.setEmc(currentEmc.subtract(toExtractFrom)); } - extracted += toExtractFrom.divide(multiplier).longValue(); + lastExtracted = toExtractFrom.divide(multiplier).longValue(); + extracted += lastExtracted; + } + + if (lastExtracted == 0) { + providers.remove(provider); } } }