Skip to content

Commit a2df9a0

Browse files
author
Developer
committed
Fix thread safety issue in ThreadedScanJobExecutor
Fixed MT_CORRECTNESS/AT_STALE_THREAD_WRITE_OF_PRIMITIVE issue where finishedProbes counter was not thread-safe. Changed finishedProbes from a primitive int to AtomicInteger to ensure thread-safe updates when multiple threads increment the counter. Also made probeCount volatile to ensure visibility across threads.
1 parent d4a3032 commit a2df9a0

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/main/java/de/rub/nds/scanner/core/execution/ThreadedScanJobExecutor.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.concurrent.Future;
2525
import java.util.concurrent.Semaphore;
2626
import java.util.concurrent.ThreadPoolExecutor;
27+
import java.util.concurrent.atomic.AtomicInteger;
2728
import org.apache.logging.log4j.LogManager;
2829
import org.apache.logging.log4j.Logger;
2930

@@ -59,8 +60,8 @@ public class ThreadedScanJobExecutor<
5960
// Used for waiting for Threads in the ThreadPoolExecutor
6061
private final Semaphore semaphore = new Semaphore(0);
6162

62-
private int probeCount;
63-
private int finishedProbes = 0;
63+
private volatile int probeCount;
64+
private final AtomicInteger finishedProbes = new AtomicInteger(0);
6465

6566
/**
6667
* Creates a new ThreadedScanJobExecutor with a custom thread pool.
@@ -139,13 +140,13 @@ private void executeProbesTillNoneCanBeExecuted(ReportT report) throws Interrupt
139140
List<Future<ScannerProbe<ReportT, StateT>>> finishedFutures = new ArrayList<>();
140141
for (Future<ScannerProbe<ReportT, StateT>> result : new ArrayList<>(futureResults)) {
141142
if (result.isDone()) {
142-
finishedProbes++;
143+
int currentFinishedProbes = finishedProbes.incrementAndGet();
143144
ScannerProbe<ReportT, StateT> probeResult = null;
144145
try {
145146
probeResult = result.get();
146147
LOGGER.info(
147148
"[{}/{}] {} probe executed",
148-
finishedProbes,
149+
currentFinishedProbes,
149150
probeCount,
150151
probeResult.getType().getName());
151152
} catch (ExecutionException e) {

0 commit comments

Comments
 (0)