Skip to content

Recursive scans keep every generated candidate name in a visited set #301

Closed

Description

@TMHSDigital

Summary

For -recursive the dispatcher records every candidate at every depth in a map that is never trimmed (about 75 MB per million candidates), and when a budget stops admission it walks every ungenerated candidate to classify it. Both follow from deduplicating by candidate instead of by parent.


Recursive scans keep every generated candidate name in the dispatcher's visited set

Where

  • internal/scan/dispatcher.go:48-53
  • internal/scan/dispatcher.go:175-192
  • internal/scan/dispatcher.go:65

Details

For cfg.Recursive the dispatcher allocates visited map[string]struct{} (dispatcher.go:48-53) and next() inserts a key for every candidate it generates, at every depth: key := entry; if e.depth > 1 { key = name[:len(name)-len(d.cfg.Domain)-1] } ... d.visited[key] = struct{}{} (dispatcher.go:178-191). At depth 1 the key is the entry itself (no extra string), but below depth 1 the key is a substring of name, which pins the whole candidate name allocation for the life of the scan. Nothing is ever removed. The assumption (confidence medium): per-entry cost is estimated from the map slot (16-byte key, zero-size value, plus control bytes and load-factor slack, about 25 to 35 bytes) plus the pinned name (32 to 48 bytes), not measured.

Impact

About 30 bytes per depth-1 candidate and about 70 to 80 bytes per deeper candidate, retained until the scan ends. Candidates number len(Entries) * (1 + names found): a 1M-line list with 20 found parents is 21M candidates, about 1.5 GB of visited set; a 100k list with 100 parents is 10M candidates, about 0.7 GB. Recursion over such lists needs -force or -max-queries (runner.go:685), and -max-queries M bounds the set to about M entries, so the cost is roughly 75 MB per million admitted candidates of a recursive scan. The non-recursive path already avoids this map on purpose (#248, dispatcher.go:61-64). Hurts from a few million candidates.

Steps to reproduce

subenum -recursive -depth 2 -max-queries 20000000 -simulate -hit-rate 1 -w <1M-line list> example.com; heap grows with processed candidates instead of staying flat after the wordlist is loaded.

Suggested fix

Children of two different parents can only collide through multi-label wordlist entries (a.b under the root equals a under b.root), because single-label entries are unique and a name then determines its (entry, parent) pair. So: dedupe expansions by parent instead of by candidate (a set with one entry per found name, which also covers a ResumeParents name that is found again), and keep the per-candidate set only when the wordlist contains an entry with a dot (detect once in newDispatcher). With no dotted entries the per-candidate map disappears entirely.


Reaching -max-queries or -max-duration in a recursive scan walks every ungenerated candidate

Where

  • internal/scan/dispatcher.go:108-136

Details

When a budget stops admission and either -exclude is set or the scan is recursive (d.visited != nil), stopAdmitting loops for _, e := range d.frontier[d.head:] { for i := e.next; i < d.n; i++ { name := entry + "." + e.parent ... } } (dispatcher.go:112-131): one string allocation, one map lookup and one scope check for every candidate that will never be queried, to split the skipped count from duplicates and exclusions (#238). It runs in the dispatcher goroutine, which meanwhile receives neither completions nor found parents.

Impact

O(queued parents * wordlist entries) at about 100 to 150 ns each. Every name found at depth 1 is queued until the root list is exhausted, so a 1M-line recursive scan that hits -max-queries or -max-duration with 50 parents queued does 51M iterations: several seconds past the time budget with workers blocked on their hand-off, and about 2 GB of short-lived garbage. Negligible for the 5k list (under 50 ms).

Steps to reproduce

subenum -recursive -depth 2 -max-duration 30s -simulate -hit-rate 1 -w <1M-line list> example.com; the run ends noticeably later than 30 s.

Suggested fix

Follows from the visited-set fix above: with duplicates tracked per parent, the remaining count per expansion is d.n - e.next when the scope is empty, and with -exclude only subtreeExcluded(e.parent) plus the exact-name patterns (a handful of map lookups per expansion) need checking, so no candidate has to be built.


Notes

Related: #51 and #248 (earlier work on the visited map; the non-recursive path no longer has one).

Acceptance criteria

Getting started

  • Relevant files: internal/scan/dispatcher.go
  • How to run tests: make test (or go test ./...). Tests must stay hermetic: use -simulate, -simulate-zone or the local DNS test server, never a live domain.
  • Related docs: docs/DEVELOPER_GUIDE.md, docs/ARCHITECTURE.md

Filed by repo-audit/full-audit at d0c56f0. Severity: medium. Effort: M. audit-key: performance|internal/scan/dispatcher.go|visited

Activity

  1. added
    help wanted
    performance
    area: scan
    severity: medium
    on Nov 15, 2026
  2. TMHSDigital commented on Nov 15, 2026

    @TMHSDigital
    OwnerAuthor

    Fixed in db61a47.

    A recursive scan now dedupes by parent: the dispatcher keeps one entry per parent it has queued, so a parent found twice is expanded once. The per-candidate set is only built when the wordlist contains a dotted entry, the one case where two parents can produce the same name. When a budget stops the scan, the remainder is counted without building its names.

    One count changes in an edge case: a resumed parent listed twice, or found again after a budget stop, used to have its children counted as skipped twice; they are now counted once.

    Verified with unit tests in internal/scan/dispatcher_test.go: TestDispatcherVisitedSetOnlyForMultiLabelEntries and TestDispatcherExpandsAResumedParentOnce fail on the old code; TestCapCountsRecursiveRemainderWithoutBuildingIt compares a capped scan's counts with an uncapped run and guards against regressions. go test ./... and golangci-lint run pass.

    Not measured: I did not run the 1M-line reproduction, so the memory and stop-time gains are unquantified. With dotted entries in a recursive scan the old per-candidate walk is kept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: scanhelp wantedperformanceseverity: medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions