GP-3685 fixing issue with cancelling table sort can cause table data

corruption
This commit is contained in:
ghidragon 2023-07-31 14:20:52 -04:00
parent 784c7ee4cf
commit 0638bfcde5

View file

@ -522,11 +522,16 @@ public class TableUpdateJob<T> {
Comparator<T> comparator = newSortContext.getComparator(); Comparator<T> comparator = newSortContext.getComparator();
Comparator<T> monitoredComparator = new MonitoredComparator<>(comparator, monitor, size); Comparator<T> monitoredComparator = new MonitoredComparator<>(comparator, monitor, size);
// copy the data. If the sort is cancelled, the data could be corrupted
List<T> copy = new ArrayList<>(data);
try { try {
Collections.sort(data, monitoredComparator); Collections.sort(data, monitoredComparator);
} }
catch (SortCancelledException e) { catch (SortCancelledException e) {
// do nothing, the old data will remain // restore copy as data could be corrupted
data.clear();
data.addAll(copy);
} }
catch (Exception e) { catch (Exception e) {
// We added this to catch an issue if the sort comparators violate the contract of // We added this to catch an issue if the sort comparators violate the contract of
@ -534,6 +539,9 @@ public class TableUpdateJob<T> {
// throw the exception. This will allow the currently loaded data to be used, albeit // throw the exception. This will allow the currently loaded data to be used, albeit
// unsorted. // unsorted.
Msg.error(this, "Unable to finish table sorting", e); Msg.error(this, "Unable to finish table sorting", e);
// restore copy as data could be corrupted
data.clear();
data.addAll(copy);
} }
monitor.setMessage("Done sorting"); monitor.setMessage("Done sorting");