Skip to content

Commit affb24f

Browse files
committed
Merge 1.18 into 1.19.2
2 parents 4c80dea + b5870b3 commit affb24f

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

forge/src/main/java/org/embeddedt/modernfix/forge/config/ConfigFixer.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ private static class LockingConfigHandler implements Consumer<IConfigEvent> {
4343

4444
@Override
4545
public void accept(IConfigEvent modConfigEvent) {
46-
synchronized (this) {
46+
Object cfgObj = NightConfigFixer.toWriteSyncConfig(modConfigEvent.getConfig().getConfigData());
47+
if(cfgObj != null) {
48+
// don't synchronize on 'this' as it produces a deadlock when used alongside NightConfigFixer
49+
synchronized (cfgObj) {
50+
this.actualHandler.accept(modConfigEvent);
51+
}
52+
} else {
53+
ModernFix.LOGGER.warn("Unable to sync on a {} config object", modConfigEvent.getConfig().getConfigData().getClass().getName());
4754
this.actualHandler.accept(modConfigEvent);
4855
}
4956
}

forge/src/main/java/org/embeddedt/modernfix/forge/config/NightConfigFixer.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static void monitorFileWatcher() {
3838
private static final Class<?> WATCHED_FILE = LamdbaExceptionUtils.uncheck(() -> Class.forName("com.electronwill.nightconfig.core.file.FileWatcher$WatchedFile"));
3939
private static final Field CHANGE_HANDLER = ObfuscationReflectionHelper.findField(WATCHED_FILE, "changeHandler");
4040

41-
private static final Class<?> WRITE_SYNC_CONFIG = LamdbaExceptionUtils.uncheck(() -> Class.forName("com.electronwill.nightconfig.core.file.WriteSyncFileConfig"));
41+
public static final Class<?> WRITE_SYNC_CONFIG = LamdbaExceptionUtils.uncheck(() -> Class.forName("com.electronwill.nightconfig.core.file.WriteSyncFileConfig"));
4242
private static final Class<?> AUTOSAVE_CONFIG = LamdbaExceptionUtils.uncheck(() -> Class.forName("com.electronwill.nightconfig.core.file.AutosaveCommentedFileConfig"));
4343
private static final Field AUTOSAVE_FILECONFIG = ObfuscationReflectionHelper.findField(AUTOSAVE_CONFIG, "fileConfig");
4444

@@ -82,24 +82,42 @@ public Object computeIfAbsent(Path key, Function<? super Path, ?> mappingFunctio
8282
}
8383
}
8484

85+
private static final Set<Class<?>> UNKNOWN_FILE_CONFIG_CLASSES = Collections.synchronizedSet(new ReferenceOpenHashSet<>());
86+
87+
public static Object toWriteSyncConfig(Object config) {
88+
try {
89+
if(WRITE_SYNC_CONFIG.isAssignableFrom(config.getClass())) {
90+
return config;
91+
} else if(AUTOSAVE_CONFIG.isAssignableFrom(config.getClass())) {
92+
FileConfig fc = (FileConfig)AUTOSAVE_FILECONFIG.get(config);
93+
return toWriteSyncConfig(fc);
94+
} else {
95+
if (UNKNOWN_FILE_CONFIG_CLASSES.add(config.getClass()))
96+
ModernFixMixinPlugin.instance.logger.warn("Unexpected FileConfig class: {}", config.getClass().getName());
97+
return null;
98+
}
99+
} catch(ReflectiveOperationException e) {
100+
return null;
101+
}
102+
}
103+
85104
static class MonitoringConfigTracker implements Runnable {
86105
private final Runnable configTracker;
87106

88107
MonitoringConfigTracker(Runnable r) {
89108
this.configTracker = r;
90109
}
91110

92-
private static final Set<Class<?>> UNKNOWN_FILE_CONFIG_CLASSES = Collections.synchronizedSet(new ReferenceOpenHashSet<>());
93-
94111
private void protectFromSaving(FileConfig config, Runnable runnable) throws ReflectiveOperationException {
95-
if(WRITE_SYNC_CONFIG.isAssignableFrom(config.getClass())) {
112+
Object writeSyncConfig = toWriteSyncConfig(config);
113+
if(writeSyncConfig != null) {
96114
// keep trying to write, releasing the config lock each time in case something else needs to lock it
97115
// for any reason
98116
while(true) {
99117
// acquiring synchronized block here should in theory prevent any other concurrent loads/saves, based
100118
// off WriteSyncFileConfig implementation
101-
synchronized (config) {
102-
if(CURRENTLY_WRITING.getBoolean(config)) {
119+
synchronized (writeSyncConfig) {
120+
if(CURRENTLY_WRITING.getBoolean(writeSyncConfig)) {
103121
ModernFixMixinPlugin.instance.logger.fatal("Config being written during load!!!");
104122
try { Thread.sleep(500); } catch(InterruptedException e) { Thread.currentThread().interrupt(); }
105123
continue;
@@ -110,12 +128,7 @@ private void protectFromSaving(FileConfig config, Runnable runnable) throws Refl
110128
break;
111129
}
112130
}
113-
} else if(AUTOSAVE_CONFIG.isAssignableFrom(config.getClass())) {
114-
FileConfig fc = (FileConfig)AUTOSAVE_FILECONFIG.get(config);
115-
protectFromSaving(fc, runnable);
116131
} else {
117-
if(UNKNOWN_FILE_CONFIG_CLASSES.add(config.getClass()))
118-
ModernFixMixinPlugin.instance.logger.warn("Unexpected FileConfig class: {}", config.getClass().getName());
119132
runnable.run();
120133
}
121134
}

0 commit comments

Comments
 (0)