Skip to content

Commit d19d310

Browse files
committed
deadlock avoidance
1 parent 0703368 commit d19d310

File tree

2 files changed

+55
-29
lines changed

2 files changed

+55
-29
lines changed

src/main/java/com/falsepattern/lib/internal/proxy/ClientProxy.java

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
import java.util.ArrayList;
2121
import java.util.List;
22+
import java.util.concurrent.Callable;
2223
import java.util.concurrent.Future;
24+
import java.util.concurrent.TimeUnit;
2325

2426
@SideOnly(Side.CLIENT)
2527
public class ClientProxy extends CommonProxy {
@@ -34,33 +36,46 @@ public void preInit(FMLPreInitializationEvent e) {
3436
@Override
3537
public void postInit(FMLPostInitializationEvent e) {
3638
super.postInit(e);
37-
chatFuture = Async.asyncWorker.submit(() -> {
38-
val updates = updatesFuture.get();
39-
if (updates == null || updates.size() == 0) return null;
40-
val updateText = new ArrayList<IChatComponent>(FormattedText.parse(I18n.format("falsepatternlib.chat.updatesavailable")).toChatText());
41-
val mods = Loader.instance().getIndexedModList();
42-
for (val update : updates) {
43-
val mod = mods.get(update.modID);
44-
updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.modname", mod.getName())).toChatText());
45-
updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.currentversion", update.currentVersion)).toChatText());
46-
updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.latestversion", update.latestVersion)).toChatText());
47-
if (!update.updateURL.isEmpty()) {
48-
val pre = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurlpre")).toChatText();
49-
val link = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurl")).toChatText();
50-
val post = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurlpost")).toChatText();
51-
pre.get(pre.size() - 1).appendSibling(link.get(0));
52-
link.get(link.size() - 1).appendSibling(post.get(0));
53-
for (val l: link) {
54-
l.getChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, update.updateURL));
39+
chatFuture = Async.asyncWorker.submit(new Callable<List<IChatComponent>>() {
40+
@Override
41+
public List<IChatComponent> call() throws Exception {
42+
//Deadlock avoidance
43+
if (updatesFuture == null || updatesFuture.isCancelled()) {
44+
chatFuture = null;
45+
return null;
46+
}
47+
if (!updatesFuture.isDone()) {
48+
chatFuture = Async.asyncWorker.submit(this);
49+
return null;
50+
}
51+
val updates = updatesFuture.get();
52+
if (updates == null || updates.size() == 0)
53+
return null;
54+
val updateText = new ArrayList<IChatComponent>(FormattedText.parse(I18n.format("falsepatternlib.chat.updatesavailable")).toChatText());
55+
val mods = Loader.instance().getIndexedModList();
56+
for (val update : updates) {
57+
val mod = mods.get(update.modID);
58+
updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.modname", mod.getName())).toChatText());
59+
updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.currentversion", update.currentVersion)).toChatText());
60+
updateText.addAll(FormattedText.parse(I18n.format("falsepatternlib.chat.latestversion", update.latestVersion)).toChatText());
61+
if (!update.updateURL.isEmpty()) {
62+
val pre = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurlpre")).toChatText();
63+
val link = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurl")).toChatText();
64+
val post = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurlpost")).toChatText();
65+
pre.get(pre.size() - 1).appendSibling(link.get(0));
66+
link.get(link.size() - 1).appendSibling(post.get(0));
67+
for (val l : link) {
68+
l.getChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, update.updateURL));
69+
}
70+
link.remove(0);
71+
post.remove(0);
72+
updateText.addAll(pre);
73+
updateText.addAll(link);
74+
updateText.addAll(post);
5575
}
56-
link.remove(0);
57-
post.remove(0);
58-
updateText.addAll(pre);
59-
updateText.addAll(link);
60-
updateText.addAll(post);
6176
}
77+
return updateText;
6278
}
63-
return updateText;
6479
});
6580
}
6681

@@ -70,7 +85,7 @@ public void onSinglePlayer(EntityJoinWorldEvent e) {
7085
!(e.entity instanceof EntityPlayerSP)) return;
7186
val player = (EntityPlayerSP) e.entity;
7287
try {
73-
for (val line: chatFuture.get()) {
88+
for (val line: chatFuture.get(1, TimeUnit.SECONDS)) {
7489
player.addChatMessage(line);
7590
}
7691
chatFuture = null;

src/main/java/com/falsepattern/lib/internal/proxy/CommonProxy.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import net.minecraftforge.common.MinecraftForge;
1717

1818
import java.util.List;
19+
import java.util.concurrent.Callable;
1920
import java.util.concurrent.ExecutionException;
2021
import java.util.concurrent.Future;
2122

@@ -36,12 +37,22 @@ public void preInit(FMLPreInitializationEvent e) {
3637
if (LibraryConfig.ENABLE_UPDATE_CHECKER) {
3738
FalsePatternLib.getLog().info("Launching asynchronous update check.");
3839
val updateCheckFuture = UpdateChecker.fetchUpdatesAsync("https://falsepattern.com/mc/versions.json");
39-
updatesFuture = Async.asyncWorker.submit(() -> {
40-
if (!updateCheckFuture.isCancelled()) {
40+
updatesFuture = Async.asyncWorker.submit(new Callable<List<ModUpdateInfo>>() {
41+
@Override
42+
public List<ModUpdateInfo> call() {
43+
//Deadlock avoidance
44+
if (updateCheckFuture.isCancelled()) {
45+
updatesFuture = null;
46+
return null;
47+
}
48+
if (!updateCheckFuture.isDone()) {
49+
updatesFuture = Async.asyncWorker.submit(this);
50+
return null;
51+
}
4152
try {
4253
var updates = updateCheckFuture.get();
4354
if (updates != null && updates.size() > 0) {
44-
for (val update:updates) {
55+
for (val update : updates) {
4556
update.log(FalsePatternLib.getLog());
4657
}
4758
} else if (updates == null) {
@@ -54,8 +65,8 @@ public void preInit(FMLPreInitializationEvent e) {
5465
} catch (InterruptedException | ExecutionException ex) {
5566
FalsePatternLib.getLog().warn("Error while checking updates", ex);
5667
}
68+
return null;
5769
}
58-
return null;
5970
});
6071
}
6172
}

0 commit comments

Comments
 (0)