From d325abdb15a78e621ead48b5b83701645de5035d Mon Sep 17 00:00:00 2001 From: bfs2021 <101915083+bfs2021@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:42:36 +0800 Subject: [PATCH] Count a capped expand's children as stopAdmitting counts its remainder (#393) A recursive scan that has reached -max-queries counted every child of a parent found after the cap as skipped: expand's capped branch added all n children to skipped without checking -exclude or the visited set, so excluded children were misreported as skipped and, with multi-label entries, children already reached from another parent were counted twice. #361 made stopAdmitting count the queued remainder exactly; a late parent now goes through the same split by queueing a temporary expansion and stopping admission again, so excluded children are excluded and visited ones are not counted twice. --- internal/scan/dispatcher.go | 9 +++++- internal/scan/dispatcher_test.go | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/internal/scan/dispatcher.go b/internal/scan/dispatcher.go index aa1a3bd..c4923dc 100644 --- a/internal/scan/dispatcher.go +++ b/internal/scan/dispatcher.go @@ -465,7 +465,14 @@ func (d *dispatcher) expand(parent job) { case d.cfg.scope.subtreeExcluded(parent.domain): d.excluded += d.n case d.capped: - d.skipped += d.n + // A parent found after the budget stopped admission: queue it and + // stop admission again, so its children are counted as the queued + // remainder is (#393): children -exclude rules out are excluded, + // not skipped, and children the visited set would drop are not + // counted twice. + d.frontier = append(d.frontier, expansion{parent: parent.domain, depth: parent.depth + 1}) + d.remaining += d.n + d.stopAdmitting() default: d.frontier = append(d.frontier, expansion{parent: parent.domain, depth: parent.depth + 1}) d.remaining += d.n diff --git a/internal/scan/dispatcher_test.go b/internal/scan/dispatcher_test.go index a1dbce9..48b1193 100644 --- a/internal/scan/dispatcher_test.go +++ b/internal/scan/dispatcher_test.go @@ -532,3 +532,57 @@ func TestCappedExpandOfExcludedSubtree(t *testing.T) { t.Errorf("excluded %d skipped %d, want the 5 children excluded and skipped unchanged at %d", d.excluded, d.skipped, skipped) } } + +// TestCappedExpandCountsExcludedChildren covers #393: a parent of a recursive +// scan that is found after a budget stopped admission has the children -exclude +// rules out counted as excluded, not skipped, as stopAdmitting counts them. +func TestCappedExpandCountsExcludedChildren(t *testing.T) { + entries := makeEntries(10) + d := testDispatcher(Config{Entries: entries, Recursive: true, MaxQueries: 5, + Exclude: []string{entries[9] + ".p0.example.com"}}, 2) + if got := len(drain(d)); got != 5 { + t.Fatalf("admitted %d, want 5", got) + } + if d.skipped != 5 || d.excluded != 0 { + t.Fatalf("before the late expand: skipped %d excluded %d, want 5 and 0", d.skipped, d.excluded) + } + d.expand(job{domain: "p0.example.com", depth: 1}) + // p0's ten children: entries[9].p0 is excluded, the other nine are skipped. + if d.excluded != 1 || d.skipped != 14 || d.admitted+d.skipped+d.excluded != 20 { + t.Errorf("after the late expand: admitted %d skipped %d excluded %d, want 5, 14 and 1 (20 candidates)", + d.admitted, d.skipped, d.excluded) + } +} + +// TestCappedExpandCountsRecursiveRepeatsOnce covers #393: a parent of a +// recursive scan found after a budget stopped admission does not have the +// children another parent already reached counted again, so admitted, skipped +// and excluded still add up to the candidates the scan with no budget +// generates, and an excluded child is excluded whichever parent reaches it. +func TestCappedExpandCountsRecursiveRepeatsOnce(t *testing.T) { + for _, exclude := range [][]string{nil, {"x.a.b.example.com"}} { + cfg := Config{Entries: []string{"x", "x.a", "a.b", "b", "y"}, Recursive: true, MaxQueries: 4, Exclude: exclude} + // The same scan with no budget, expanding both parents. + full := cfg + full.MaxQueries = 0 + all := testDispatcher(full, 3) + roots := len(drain(all)) + all.expand(job{domain: "b.example.com", depth: 1}) + all.expand(job{domain: "a.b.example.com", depth: 1}) + candidates := roots + len(drain(all)) + all.excluded + + d := testDispatcher(cfg, 3) + admitted := len(drain(d)) + // Both parents resolve after the cap: b first, a.b second, so + // x.a.b.example.com is reached from both and counted once. + d.expand(job{domain: "b.example.com", depth: 1}) + d.expand(job{domain: "a.b.example.com", depth: 1}) + if admitted != 4 || !d.capped { + t.Fatalf("%v: admitted %d capped %v, want 4 and true", exclude, admitted, d.capped) + } + if d.skipped != candidates-admitted-all.excluded || d.excluded != all.excluded || admitted+d.skipped+d.excluded != candidates { + t.Errorf("%v: skipped %d excluded %d, want %d and %d (of %d candidates)", + exclude, d.skipped, d.excluded, candidates-admitted-all.excluded, all.excluded, candidates) + } + } +}