}
}
if (deadTime < (TickThreading.instance.deadLockTime * 1000000000l)) {
return true;
}
final MinecraftServer minecraftServer = MinecraftServer.getServer();
if (!minecraftServer.isServerRunning() || minecraftServer.isServerStopped()) {
return false;
}
if (!spikeDetector && minecraftServer.currentlySaving.get() != 0) {
Log.severe("The server seems to have frozen while saving - Waiting for two minutes to give it time to complete.");
InsecurityManager.flushLogs();
trySleep(180000);
if (minecraftServer.currentlySaving.get() != 0) {
Log.info("Server still seems to be saving, must've deadlocked.");
minecraftServer.currentlySaving.set(0);
} else {
return true;
}
}
TreeMap<String, String> sortedThreads = new TreeMap<String, String>();
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
if (!attemptedToRecoverDeadlock) {
StringBuilder sb = new StringBuilder();
sb
.append("The server appears to have ").append(spikeDetector ? "lag spiked." : "deadlocked.")
.append("\nLast tick ").append(deadTime / 1000000000).append("s ago.");
String prefix = "\nWaiting ThreadManagers: ";
Set<String> threadManagerSet = new HashSet<String>();
for (ThreadManager threadManager : DeadLockDetector.threadManagers) {
if (threadManager.isWaiting()) {
threadManagerSet.add(threadManager.getName());
}
}
for (ThreadManager threadManager : DeadLockDetector.threadManagers) {
if (threadManager.isWaiting()) {
threadManagerSet.remove(threadManager.getParentName());
}
}
for (String threadManager : threadManagerSet) {
sb.append(prefix).append(threadManager);
prefix = ", ";
}
sb.append("\n\n");
long[] deadlockedThreads = threadMXBean.findDeadlockedThreads();
if (deadlockedThreads == null) {
LoadingCache<String, List<ThreadInfo>> threads = CacheBuilder.newBuilder().build(new CacheLoader<String, List<ThreadInfo>>() {
@Override
public List<ThreadInfo> load(final String key) throws Exception {
return new ArrayList<ThreadInfo>();
}
});
ThreadInfo[] t = threadMXBean.dumpAllThreads(true, true);
for (ThreadInfo thread : t) {
String info = toString(thread, false);
if (info != null) {
threads.getUnchecked(info).add(thread);
}
}
for (Map.Entry<String, List<ThreadInfo>> entry : threads.asMap().entrySet()) {
List<ThreadInfo> threadInfoList = entry.getValue();
ThreadInfo lowest = null;
for (ThreadInfo threadInfo : threadInfoList) {
if (lowest == null || threadInfo.getThreadName().toLowerCase().compareTo(lowest.getThreadName().toLowerCase()) < 0) {
lowest = threadInfo;
}
}
List threadNameList = CollectionsUtil.newList(threadInfoList, new Function<Object, Object>() {
@Override
public Object apply(final Object input) {
return ((ThreadInfo) input).getThreadName();
}
});
Collections.sort(threadNameList);
sortedThreads.put(lowest.getThreadName(), '"' + CollectionsUtil.join(threadNameList, "\", \"") + "\" " + entry.getKey());
}
sb.append(CollectionsUtil.join(sortedThreads.values(), "\n"));
} else {
ThreadInfo[] infos = threadMXBean.getThreadInfo(deadlockedThreads, true, true);
sb.append("Definitely deadlocked: \n");
for (ThreadInfo threadInfo : infos) {
sb.append(toString(threadInfo, true)).append('\n');
}
}
if (!spikeDetector) {
sb.append("\nAttempting to recover without restarting.");
for (String threadManager : threadManagerSet) {
tryFixDeadlocks(threadManager);
}
}
Log.severe(sb.toString());
attemptedToRecoverDeadlock = true;
trySleep(15000);
return true;
}
if (spikeDetector) {
return true;
}
if (TickThreading.instance.exitOnDeadlock) {
sendChatSafely(ChatFormat.RED + TickThreading.instance.messageDeadlockSavingExiting);
}
Log.severe("Failed to recover from the deadlock.");
InsecurityManager.flushLogs();
if (!TickThreading.instance.exitOnDeadlock) {
Log.severe("Now attempting to save the world. The server will not stop, you must do this yourself. If you want the server to stop automatically on deadlock, enable exitOnDeadlock in TT's config.");
minecraftServer.saveEverything();
return false;
}
// Yes, we save multiple times - handleServerStopping may freeze on the same thing we deadlocked on, but if it doesn't might change stuff
// which needs to be saved.
minecraftServer.getNetworkThread().stopListening();
trySleep(500);
new Thread() {
@Override
public void run() {
// We can't lock here, deadlock may be due to the playerEntityList lock.
int attempts = 5;
while (attempts-- > 0) {
try {
for (EntityPlayerMP entityPlayerMP : new ArrayList<EntityPlayerMP>(minecraftServer.getConfigurationManager().playerEntityList)) {
entityPlayerMP.playerNetServerHandler.kickPlayerFromServer("Restarting");
}
attempts = 0;
} catch (ConcurrentModificationException ignored) {
}
}
}
}.start();
trySleep(1000);
Log.info("Attempting to save");
InsecurityManager.flushLogs();
new Thread() {
@Override
public void run() {
trySleep(300000);
Log.severe("Froze while attempting to stop - halting server.");
InsecurityManager.flushLogs();
new Thread() {
@Override
public void run() {
trySleep(150000);
Log.severe("Something really broke... Runtime.exit() failed to stop the server. Crashing the JVM now.");
InsecurityManager.flushLogs();
UnsafeUtil.crashMe();
}
}.start();
Runtime.getRuntime().exit(1);
}
}.start();
minecraftServer.saveEverything(); // Save first
Log.info("Saved, now attempting to stop the server and disconnect players cleanly");
try {
minecraftServer.getConfigurationManager().disconnectAllPlayers("The server has frozen and must now restart.");
minecraftServer.stopServer();
FMLCommonHandler.instance().handleServerStopping(); // Try to get mods to save data - this may lock up, as we deadlocked.
} catch (Throwable throwable) {
Log.severe("Error stopping server", throwable);
}
minecraftServer.saveEverything(); // Save again, in case they changed anything.
minecraftServer.initiateShutdown();
InsecurityManager.flushLogs();
trySleep(1000);
Runtime.getRuntime().exit(1);
return false;
}