jscanner

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 7ccfef46548f208cec2b743f21ad9dca664eedee
parent dcc9918ee18a3fe4a232004672f1caa52ea8008c
Author: Matthias Jaros <jaros@mailbox.org>
Date:   Wed,  8 Jul 2026 19:22:47 +0200

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

Diffstat:
Msrc/main/java/com/it_jaros/jscanner/Scanner.java | 79+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
1 file 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 @@ -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 {