Fix concurrent modification bug in delete method

This commit is contained in:
0xboobface 2018-12-01 14:01:29 +01:00
parent b44a1c2422
commit 53f77afb85
1 changed files with 31 additions and 20 deletions

View File

@ -23,6 +23,8 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -93,6 +95,7 @@ public class RecordingsTab extends Tab implements TabSelectionListener {
ContextMenu popup; ContextMenu popup;
ProgressBar spaceLeft; ProgressBar spaceLeft;
Label spaceLabel; Label spaceLabel;
Lock recordingsLock = new ReentrantLock();
public RecordingsTab(String title, Recorder recorder, Config config, List<Site> sites) { public RecordingsTab(String title, Recorder recorder, Config config, List<Site> sites) {
super(title); super(title);
@ -165,14 +168,12 @@ public class RecordingsTab extends Tab implements TabSelectionListener {
setStyle(null); setStyle(null);
} else { } else {
setText(StringUtil.formatSize(sizeInByte)); setText(StringUtil.formatSize(sizeInByte));
setStyle("-fx-alignment: CENTER-RIGHT;");
if(Objects.equals(System.getenv("CTBREC_DEV"), "1")) { if(Objects.equals(System.getenv("CTBREC_DEV"), "1")) {
int row = this.getTableRow().getIndex(); int row = this.getTableRow().getIndex();
JavaFxRecording rec = tableViewProperty().get().getItems().get(row); JavaFxRecording rec = tableViewProperty().get().getItems().get(row);
if(!rec.valueChanged() && rec.getStatus() == STATUS.RECORDING) { if(!rec.valueChanged() && rec.getStatus() == STATUS.RECORDING) {
setStyle("-fx-alignment: CENTER-RIGHT; -fx-background-color: red"); setStyle("-fx-alignment: CENTER-RIGHT; -fx-background-color: red");
} else {
setStyle("-fx-alignment: CENTER-RIGHT;");
//setStyle(null);
} }
} }
} }
@ -280,6 +281,8 @@ public class RecordingsTab extends Tab implements TabSelectionListener {
return; return;
} }
recordingsLock.lock();
try {
for (Iterator<JavaFxRecording> iterator = observableRecordings.iterator(); iterator.hasNext();) { for (Iterator<JavaFxRecording> iterator = observableRecordings.iterator(); iterator.hasNext();) {
JavaFxRecording old = iterator.next(); JavaFxRecording old = iterator.next();
if (!recordings.contains(old)) { if (!recordings.contains(old)) {
@ -298,6 +301,9 @@ public class RecordingsTab extends Tab implements TabSelectionListener {
old.update(recording); old.update(recording);
} }
} }
} finally {
recordingsLock.unlock();
}
table.sort(); table.sort();
} }
@ -552,20 +558,25 @@ public class RecordingsTab extends Tab implements TabSelectionListener {
Thread deleteThread = new Thread() { Thread deleteThread = new Thread() {
@Override @Override
public void run() { public void run() {
recordingsLock.lock();
try { try {
for (JavaFxRecording r : recordings) { List<Recording> deleted = new ArrayList<>();
for (Iterator<JavaFxRecording> iterator = recordings.iterator(); iterator.hasNext();) {
JavaFxRecording r = iterator.next();
if(r.getStatus() != STATUS.FINISHED) { if(r.getStatus() != STATUS.FINISHED) {
continue; continue;
} }
try { try {
recorder.delete(r); recorder.delete(r);
Platform.runLater(() -> observableRecordings.remove(r)); deleted.add(r);
} catch (IOException | InvalidKeyException | NoSuchAlgorithmException | IllegalStateException e1) { } catch (IOException | InvalidKeyException | NoSuchAlgorithmException | IllegalStateException e1) {
LOG.error("Error while deleting recording", e1); LOG.error("Error while deleting recording", e1);
showErrorDialog("Error while deleting recording", "Recording not deleted", e1); showErrorDialog("Error while deleting recording", "Recording not deleted", e1);
} }
} }
observableRecordings.removeAll(deleted);
} finally { } finally {
recordingsLock.unlock();
Platform.runLater(() -> table.setCursor(Cursor.DEFAULT)); Platform.runLater(() -> table.setCursor(Cursor.DEFAULT));
} }
} }