fix: add timeout to reading history from exchanger (#2920)

This commit is contained in:
Jordan 2024-09-19 05:55:53 +01:00 committed by GitHub
parent 7e75ce78ec
commit 393f80165c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,6 +4,8 @@ import com.sk89q.worldedit.history.change.Change;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
import java.util.concurrent.Exchanger; import java.util.concurrent.Exchanger;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
/** /**
@ -32,8 +34,9 @@ public class ChangeExchangeCoordinator implements AutoCloseable {
.start(() -> this.runnerTask.accept(this.exchanger, new Change[length])); .start(() -> this.runnerTask.accept(this.exchanger, new Change[length]));
} }
try { try {
return exchanger.exchange(consumed); // Allow a reasonable timeout in case of weirdness
} catch (InterruptedException e) { return exchanger.exchange(consumed, 30, TimeUnit.SECONDS);
} catch (InterruptedException | TimeoutException e) {
this.runner.interrupt(); this.runner.interrupt();
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
return null; return null;