<commit_before>package nallar.tickthreading.minecraft;

import com.google.common.base.Function;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import cpw.mods.fml.common.FMLCommonHandler;
import nallar.exception.ThreadStuckError;
import nallar.tickthreading.Log;
import nallar.tickthreading.util.ChatFormat;
import nallar.tickthreading.util.CollectionsUtil;
import nallar.unsafe.UnsafeUtil;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.packet.Packet3Chat;
import net.minecraft.server.MinecraftServer;

import java.lang.management.*;
import java.util.*;
import java.util.concurrent.*;

public class DeadLockDetector {
	private static final StackTraceElement[] EMPTY_STACK_TRACE = new StackTraceElement[0];
	private boolean attemptedToRecoverDeadlock = false;
	private boolean sentWarningRecently = false;
	private static volatile long lastTickTime = 0;
	public static final Set<ThreadManager> threadManagers = Collections.newSetFromMap(new ConcurrentHashMap<ThreadManager, Boolean>());
	private final boolean spikeDetector = TickThreading.instance.deadLockTime == 1;

	public DeadLockDetector() {
		final int sleepTime = Math.max(1000, (TickThreading.instance.deadLockTime * 1000) / 6);
		Thread deadlockThread = new Thread(new Runnable() {
			@Override
			public void run() {
				trySleep(60000);
				while (checkForDeadlocks()) {
					trySleep(sleepTime);
				}
			}
		});
		deadlockThread.setName("Deadlock Detector");
		deadlockThread.start();
	}

	public static void tickAhead(int seconds) {
		tick(System.nanoTime() + seconds * 10000000000l);
	}

	public static synchronized long tick(long nanoTime) {
		long lastTickTime_ = lastTickTime;
		if (lastTickTime_ == 0) {
			lastTickTime = nanoTime + 10 * 10000000000l;
		} else if (lastTickTime_ < nanoTime) {
			lastTickTime = nanoTime;
		}
		return nanoTime;
	}

	public static void sendChatSafely(final String message) {
		// This might freeze, if the deadlock was related to the playerlist, so do it in another thread.
		new Thread() {
			@Override
			public void run() {
				MinecraftServer.getServerConfigurationManager(MinecraftServer.getServer())
						.sendPacketToAllPlayers(new Packet3Chat(message));
			}
		}.start();
	}

	private static void tryFixDeadlocks(String stuckManagerName) {
		stuckManagerName += " - ";
		Iterable<Thread> threads = Thread.getAllStackTraces().keySet();
		int found = 0;
		int notRunning = 0;
		int killed = 0;
		for (Thread thread : threads) {
			if (thread.getName().startsWith(stuckManagerName)) {
				if (killed > 0) {
					trySleep(5);
				}
				StackTraceElement[] stackTraceElements = thread.getStackTrace();
				int runCount = 0;
				for (StackTraceElement stackTraceElement : stackTraceElements) {
					if ("run".equals(stackTraceElement.getMethodName())) {
						runCount++;
					}
				}
				if (runCount >= 3) {
					UnsafeUtil.stopThread(thread, new ThreadStuckError("Deadlock detected, appears to be caused by " + stuckManagerName));<commit_msg>Fix client crash due to sending invalid chat messages<commit_after>package nallar.tickthreading.minecraft;

import com.google.common.base.Function;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import cpw.mods.fml.common.FMLCommonHandler;
import nallar.exception.ThreadStuckError;
import nallar.tickthreading.Log;
import nallar.tickthreading.util.ChatFormat;
import nallar.tickthreading.util.CollectionsUtil;
import nallar.unsafe.UnsafeUtil;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.packet.Packet3Chat;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatMessageComponent;

import java.lang.management.*;
import java.util.*;
import java.util.concurrent.*;

public class DeadLockDetector {
	private static final StackTraceElement[] EMPTY_STACK_TRACE = new StackTraceElement[0];
	private boolean attemptedToRecoverDeadlock = false;
	private boolean sentWarningRecently = false;
	private static volatile long lastTickTime = 0;
	public static final Set<ThreadManager> threadManagers = Collections.newSetFromMap(new ConcurrentHashMap<ThreadManager, Boolean>());
	private final boolean spikeDetector = TickThreading.instance.deadLockTime == 1;

	public DeadLockDetector() {
		final int sleepTime = Math.max(1000, (TickThreading.instance.deadLockTime * 1000) / 6);
		Thread deadlockThread = new Thread(new Runnable() {
			@Override
			public void run() {
				trySleep(60000);
				while (checkForDeadlocks()) {
					trySleep(sleepTime);
				}
			}
		});
		deadlockThread.setName("Deadlock Detector");
		deadlockThread.start();
	}

	public static void tickAhead(int seconds) {
		tick(System.nanoTime() + seconds * 10000000000l);
	}

	public static synchronized long tick(long nanoTime) {
		long lastTickTime_ = lastTickTime;
		if (lastTickTime_ == 0) {
			lastTickTime = nanoTime + 10 * 10000000000l;
		} else if (lastTickTime_ < nanoTime) {
			lastTickTime = nanoTime;
		}
		return nanoTime;
	}

	public static void sendChatSafely(final String message) {
		// This might freeze, if the deadlock was related to the playerlist, so do it in another thread.
		new Thread() {
			@Override
			public void run() {
				MinecraftServer.getServerConfigurationManager(MinecraftServer.getServer())
						.sendPacketToAllPlayers(new Packet3Chat(ChatMessageComponent.createFromText(message)));
			}
		}.start();
	}

	private static void tryFixDeadlocks(String stuckManagerName) {
		stuckManagerName += " - ";
		Iterable<Thread> threads = Thread.getAllStackTraces().keySet();
		int found = 0;
		int notRunning = 0;
		int killed = 0;
		for (Thread thread : threads) {
			if (thread.getName().startsWith(stuckManagerName)) {
				if (killed > 0) {
					trySleep(5);
				}
				StackTraceElement[] stackTraceElements = thread.getStackTrace();
				int runCount = 0;
				for (StackTraceElement stackTraceElement : stackTraceElements) {
					if ("run".equals(stackTraceElement.getMethodName())) {
						runCount++;
					}
				}
				if (runCount >= 3) {
					UnsafeUtil.stopThread(thread, new ThreadStuckError("Deadlock detected, appears to be caused by " + stuckManagerName));
{'fraction_non_alphanumeric': 0.10295291300877893, 'fraction_numerical': 0.016280925778132484, 'mean_word_length': 4.785780240073869, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3402925', 'n_tokens_mistral': 2183, 'n_tokens_neox': 1875, 'n_words': 441}