summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matthias Jaros <jarlucmat@mailbox.org>2026-07-08 19:22:47 +0200
committerGravatar Matthias Jaros <jarlucmat@mailbox.org>2026-07-09 09:59:29 +0200
commit6f1cc94e412888a7d2ea60b3fa595d049e7babd3 (patch)
treeedf89292da1469c1cb0c03b09e32a9b2d1c9b510
parent8cc1e389d1ac9a2d92c8976767be8f3273336dc1 (diff)
Changed thread counting. Currently it was actually counting submitted
but not really running threads. So now it is more clear what is going on and what is being counted
-rw-r--r--src/main/java/com/it_jaros/jscanner/Scanner.java79
1 files changed, 45 insertions, 34 deletions
diff --git a/src/main/java/com/it_jaros/jscanner/Scanner.java b/src/main/java/com/it_jaros/jscanner/Scanner.java
index f656854..e22f18c 100644
--- a/src/main/java/com/it_jaros/jscanner/Scanner.java
+++ b/src/main/java/com/it_jaros/jscanner/Scanner.java
@@ -69,11 +69,13 @@ public class Scanner implements AutoCloseable {
maxHostsLimit,
host -> () -> {
try {
+ scan.getThreadCounter().inc();
scan.getHostCounter().inc();
scan.getHostTotalCounter().inc();
if (cancelled) return null;
return scanHostPorts(host, scan);
} finally {
+ scan.getThreadCounter().dec();
scan.getHostCounter().dec();
}
},
@@ -94,21 +96,23 @@ public class Scanner implements AutoCloseable {
continue;
}
- // No matter what happens we have to free the resources after getting ScanResult
+ ScanResult result = null;
try {
- ScanResult result = finishedHost.get();
- if (result != null) {
- consumer.accept(result);
- }
+ result = finishedHost.get();
+ } catch (ExecutionException e) {
+ System.err.printf("%s -> %s%n", e.getClass().getSimpleName(), e.getMessage());
} finally {
+ // No matter what happens we have to free the resources after getting ScanResult
state.activeWorkers().release();
state.inPipeline().decrementAndGet();
- scan.getThreadCounter().dec();
+ }
+
+ // release workers first before consuming
+ if (result != null) {
+ consumer.accept(result);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
- } catch (ExecutionException e) {
- System.err.printf("%s -> %s%n", e.getClass().getSimpleName(), e.getMessage());
}
}
@@ -140,9 +144,14 @@ public class Scanner implements AutoCloseable {
portRange,
maxWorkersPerHost,
port -> () -> {
- waitForSlot(portSlotFactory);
- if (cancelled) return null;
- return checkPort(host, port, scan);
+ try {
+ scan.getThreadCounter().inc();
+ waitForSlot(portSlotFactory);
+ if (cancelled) return null;
+ return checkPort(host, port, scan);
+ } finally {
+ scan.getThreadCounter().dec();
+ }
},
scan.getThreadCounter()
);
@@ -157,35 +166,39 @@ public class Scanner implements AutoCloseable {
continue;
}
+ PortResult portResult = null;
try {
- PortResult portResult = portResultFuture.get();
- if (portResult == null) continue;
- switch (portResult.getState()) {
- case OPEN -> {
- // bitset is not thread-safe, so it is set
- // outside the other virtual threads that update progress
- openPorts.set(portResult.getPort());
- serviceTypes.put(portResult.getPort(), ServiceDetector.detect(portResult.getBanner()));
- }
- case FILTERED -> {
- filteredPorts.set(portResult.getPort());
- }
- default -> {
- // sonarcube glücklich machen
- }
- }
- Exception e = portResult.getException();
- if (e != null) {
- scanFailures.add(new ScanFailure(portResult.getPort(), ExceptionInfo.from(e)));
- }
+ portResult = portResultFuture.get();
} catch (ExecutionException e) {
// something more serious did not work
System.err.printf("%s -> %s%n", e.getClass().getSimpleName(), e.getMessage());
} finally {
- scan.getThreadCounter().dec();
state.activeWorkers().release();
state.inPipeline().decrementAndGet();
}
+
+ if (portResult == null) {
+ continue;
+ }
+
+ switch (portResult.getState()) {
+ case OPEN -> {
+ // bitset is not thread-safe, so it is set
+ // outside the other virtual threads that update progress
+ openPorts.set(portResult.getPort());
+ serviceTypes.put(portResult.getPort(), ServiceDetector.detect(portResult.getBanner()));
+ }
+ case FILTERED -> {
+ filteredPorts.set(portResult.getPort());
+ }
+ default -> {
+ // sonarcube glücklich machen
+ }
+ }
+ Exception e = portResult.getException();
+ if (e != null) {
+ scanFailures.add(new ScanFailure(portResult.getPort(), ExceptionInfo.from(e)));
+ }
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
@@ -232,7 +245,6 @@ public class Scanner implements AutoCloseable {
final INPUT item = queue.next();
Callable<OUTPUT> task = taskFactory.apply(item);
inPipeline.incrementAndGet();
- threadCounter.inc();
try {
// here we are filling the completion service
// host <-> virtual thread
@@ -240,7 +252,6 @@ public class Scanner implements AutoCloseable {
submitted = true;
} catch (Throwable e) {
inPipeline.decrementAndGet();
- threadCounter.dec();
throw e;
}
} finally {