Skip to content

Commit e6ff3d2

Browse files
committed
Fix racing condition when creating snapshot while generating new chunks
1 parent 97c0220 commit e6ff3d2

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ tasks.named('wrapper', Wrapper).configure {
1515

1616
jarJar.enable()
1717

18-
var mod_version = "custom-build"
18+
var mod_version = "0.0.0-custom-build"
1919

2020
var gitTag = System.getProperty("git.tag")
2121
if (gitTag != null && !gitTag.isEmpty()) {

src/main/java/top/seraphjack/rmc/MinecraftAdapter.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package top.seraphjack.rmc;
22

33
import java.util.List;
4+
import java.util.concurrent.CountDownLatch;
45

56
import net.minecraft.network.chat.Component;
67
import net.minecraft.server.MinecraftServer;
@@ -31,10 +32,21 @@ private void logAndBroadcastMessage(String message, Object... args) {
3132

3233
@Override
3334
public void preBackup() {
34-
logAndBroadcastMessage("Backup started");
35-
server.saveEverything(true, true, true);
36-
for (ServerLevel level : server.getAllLevels()) {
37-
level.noSave = true;
35+
final CountDownLatch latch = new CountDownLatch(1);
36+
final Runnable backupAndSetNoSave = () -> {
37+
logAndBroadcastMessage("Backup started");
38+
server.saveEverything(true, true, true);
39+
for (ServerLevel level : server.getAllLevels()) {
40+
level.noSave = true;
41+
}
42+
latch.countDown();
43+
};
44+
server.executeIfPossible(backupAndSetNoSave);
45+
46+
try {
47+
latch.await();
48+
} catch (InterruptedException e) {
49+
throw new RuntimeException(e);
3850
}
3951
}
4052

src/main/java/top/seraphjack/rmc/RMCCommand.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public final class RMCCommand {
3030

3131
public static final DynamicCommandExceptionType ERROR_INVALID_INTERVAL =
3232
new DynamicCommandExceptionType(msg -> new LiteralMessage("Invalid ISO8601 duration: " + msg));
33-
private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
33+
private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
3434
private static final Logger log = LoggerFactory.getLogger(RMCCommand.class);
3535

3636
static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
@@ -90,12 +90,14 @@ private static int listSnapshots(CommandContext<CommandSourceStack> context) {
9090

9191
private static int restore(CommandContext<CommandSourceStack> context) {
9292
final var snapshot = StringArgumentType.getString(context, "snapshot");
93-
try {
94-
RMC.backupCore.restore(snapshot);
95-
} catch (Exception e) {
96-
log.error("Error restoring snapshot", e);
97-
throw new RuntimeException(e);
98-
}
93+
new Thread(() -> {
94+
try {
95+
RMC.backupCore.restore(snapshot);
96+
} catch (Exception e) {
97+
log.error("Error restoring snapshot", e);
98+
throw new RuntimeException(e);
99+
}
100+
}, "RMC-Restore-Worker").start();
99101
return SINGLE_SUCCESS;
100102
}
101103

0 commit comments

Comments
 (0)