Ensure the context class loader is set for the resource reload executors

This commit is contained in:
embeddedt 2023-05-08 09:57:53 -04:00
parent d8d76c00c7
commit 2f8f47ae3e
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 51 additions and 7 deletions

View File

@ -9,10 +9,11 @@ import org.apache.logging.log4j.Logger;
import org.embeddedt.modernfix.command.ModernFixCommands;
import org.embeddedt.modernfix.core.ModernFixMixinPlugin;
import org.embeddedt.modernfix.platform.ModernFixPlatformHooks;
import org.embeddedt.modernfix.resources.ReloadExecutor;
import org.embeddedt.modernfix.util.ClassInfoManager;
import java.lang.management.ManagementFactory;
import java.util.concurrent.*;
import java.util.concurrent.Executor;
// The value here should match an entry in the META-INF/mods.toml file
public class ModernFix {
@ -31,12 +32,7 @@ public class ModernFix {
static {
if(ModernFixMixinPlugin.instance.isOptionEnabled("perf.dedicated_reload_executor.ReloadExecutor")) {
try {
resourceReloadService = Util.makeExecutor("ResourceReload");
} catch(Throwable e) {
LOGGER.error("Error creating resource reload service, using fallback", e);
resourceReloadService = Util.backgroundExecutor();
}
resourceReloadService = ReloadExecutor.createCustomResourceReloadExecutor();
} else {
resourceReloadService = Util.backgroundExecutor();
}

View File

@ -0,0 +1,48 @@
package org.embeddedt.modernfix.resources;
import net.minecraft.ReportedException;
import net.minecraft.server.Bootstrap;
import org.embeddedt.modernfix.ModernFix;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;
import java.util.concurrent.atomic.AtomicInteger;
public class ReloadExecutor {
public static ExecutorService createCustomResourceReloadExecutor() {
ClassLoader loader = ReloadExecutor.class.getClassLoader();
AtomicInteger workerCount = new AtomicInteger(0);
return new ForkJoinPool(ForkJoinPool.getCommonPoolParallelism(), (forkJoinPool) -> {
ForkJoinWorkerThread forkJoinWorkerThread = new ForkJoinWorkerThread(forkJoinPool) {
protected void onTermination(Throwable throwOnTermination) {
if (throwOnTermination != null) {
ModernFix.LOGGER.warn("{} died", this.getName(), throwOnTermination);
} else {
ModernFix.LOGGER.debug("{} shutdown", this.getName());
}
super.onTermination(throwOnTermination);
}
};
// needed to prevent weirdness on some systems
forkJoinWorkerThread.setContextClassLoader(loader);
forkJoinWorkerThread.setName("Worker-ResourceReload-" + workerCount.getAndIncrement());
return forkJoinWorkerThread;
}, ReloadExecutor::handleException, true);
}
private static void handleException(Thread thread, Throwable throwable) {
if (throwable instanceof CompletionException) {
throwable = throwable.getCause();
}
if (throwable instanceof ReportedException) {
Bootstrap.realStdoutPrintln(((ReportedException)throwable).getReport().getFriendlyReport());
System.exit(-1);
}
ModernFix.LOGGER.error(String.format("Caught exception in thread %s", thread), throwable);
}
}