From 229e03efff106e2e362b980ced8d58e4452a572c Mon Sep 17 00:00:00 2001 From: Nicolas Roduit Date: Fri, 27 Dec 2024 14:10:21 +0100 Subject: [PATCH] Try to fix Potential Thread Synchronization Issue in LoadSeries.java #618 --- .../org/weasis/core/api/media/data/Series.java | 16 ++++++---------- .../weasis/dicom/explorer/wado/LoadSeries.java | 14 +++++++++----- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/weasis-core/src/main/java/org/weasis/core/api/media/data/Series.java b/weasis-core/src/main/java/org/weasis/core/api/media/data/Series.java index 2492c826d..46bd5025c 100755 --- a/weasis-core/src/main/java/org/weasis/core/api/media/data/Series.java +++ b/weasis-core/src/main/java/org/weasis/core/api/media/data/Series.java @@ -191,7 +191,7 @@ public final int getImageIndex(E source, Filter filter, Comparator sort) { return -1; } Iterable list = getMedias(filter, sort); - synchronized (this) { + synchronized (medias) { int index = 0; for (E e : list) { if (e == source) { @@ -242,8 +242,7 @@ public final E getMedia(int index, Filter filter, Comparator sort) { } @Override - public void dispose() { - // forEach implement synchronized + public synchronized void dispose() { medias.forEach( m -> { if (m instanceof ImageElement imageElement) { @@ -319,10 +318,8 @@ public void firePropertyChange(final ObservableEvent event) { } @Override - public int size(Filter filter) { - synchronized (this) { - return filter == null ? medias.size() : Filter.size(filter.filter(medias)); - } + public synchronized int size(Filter filter) { + return filter == null ? medias.size() : Filter.size(filter.filter(medias)); } @Override @@ -416,10 +413,9 @@ public void setFocused(boolean focused) { public boolean hasMediaContains(TagW tag, Object val) { if (val != null) { - synchronized (this) { + synchronized (medias) { for (E media : medias) { - Object val2 = media.getTagValue(tag); - if (val.equals(val2)) { + if (val.equals(media.getTagValue(tag))) { return true; } } diff --git a/weasis-dicom/weasis-dicom-explorer/src/main/java/org/weasis/dicom/explorer/wado/LoadSeries.java b/weasis-dicom/weasis-dicom-explorer/src/main/java/org/weasis/dicom/explorer/wado/LoadSeries.java index 85af17a76..b6347f9fa 100755 --- a/weasis-dicom/weasis-dicom-explorer/src/main/java/org/weasis/dicom/explorer/wado/LoadSeries.java +++ b/weasis-dicom/weasis-dicom-explorer/src/main/java/org/weasis/dicom/explorer/wado/LoadSeries.java @@ -38,6 +38,7 @@ import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Function; import java.util.stream.Collectors; @@ -140,6 +141,7 @@ public enum Status { private final AtomicInteger errors; private volatile boolean hasError = false; + private final AtomicBoolean seriesInitialized = new AtomicBoolean(false); public LoadSeries( DicomSeries dicomSeries, @@ -858,6 +860,10 @@ private File getDicomTmpDir() { /** Download file. */ private boolean process() throws IOException, URISyntaxException { + boolean firstImage = + dicomSeries != null + && dicomSeries.size(null) == 0 + && seriesInitialized.compareAndSet(false, true); boolean cache = true; File tempFile = null; DicomMediaIO dicomReader = null; @@ -904,7 +910,7 @@ private boolean process() throws IOException, URISyntaxException { FileUtil.safeClose(stream); dicomReader = new DicomMediaIO(tempFile); - if (dicomReader.isReadableDicom() && dicomSeries.size(null) == 0) { + if (dicomReader.isReadableDicom() && firstImage) { // Override the group (patient, study and series) by the dicom fields except the UID of // the group MediaSeriesGroup patient = dicomModel.getParent(dicomSeries, DicomModel.patient); @@ -941,7 +947,7 @@ private boolean process() throws IOException, URISyntaxException { final DicomMediaIO reader = dicomReader; // Necessary to wait the runnable because the dicomSeries must be added to the // dicomModel before reaching done() of SwingWorker - GuiExecutor.invokeAndWait(() -> updateUI(reader)); + GuiExecutor.invokeAndWait(() -> updateUI(reader, firstImage)); } else if (reading == Reading.ERROR) { errors.incrementAndGet(); } @@ -1098,15 +1104,13 @@ public int writFile(InputStream in, File tempFile, int[] overrideList) } } - private void updateUI(final DicomMediaIO reader) { - boolean firstImageToDisplay; + private void updateUI(final DicomMediaIO reader, boolean firstImageToDisplay) { Function buildSpecialElement = factory -> factory.buildDicomSpecialElement(reader); DicomMediaIO.ResultContainer result = reader.getMediaElement(buildSpecialElement); DicomImageElement[] medias = result.getImage(); if (medias != null) { - firstImageToDisplay = dicomSeries.size(null) == 0; if (firstImageToDisplay) { MediaSeriesGroup patient = dicomModel.getParent(dicomSeries, DicomModel.patient); if (patient != null) {