#3512·headscale

[Bug] 0.29: ViaRoutesForPeer re-resolves the whole policy for every viewer/peer pair; netmap never delivered to nodes with ~950 peers

Author: barthdvsCreated Sep 29, 2026Updated Sep 29, 2026
Labelsbug

Is this a support request?

  • This is not a support request

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

After upgrading 0.26.1 → 0.27.1 → 0.28.0 → 0.29.4 (one step at a time), our two HA subnet routers never received a netmap again. We had to roll back to 0.26.1 by restoring the database. The policy uses no via grant, no grants at all, and no autogroup:self — only groups, hosts, tagOwners and acls.

Tailnet size

  • 1026 nodes (100–180 online), 685 users, PostgreSQL
  • 2 HA subnet routers advertising ~60 routes each. They have 946 peers each; the average node has 5.9 peers (/debug/nodestore)
  • Policy: 50 acls rules, 341 destinations in total, 54 groups listing 2056 usernames

Symptoms on 0.29.4

  • headscale CPU: 4–9 cores (0.2–0.6 on 0.26.1, ~1.3 on 0.27.1 and 0.28.0)
  • Almost every /machine/map request ends with bytes=0
  • Routers register fine (machineAuthorized=true), then never reach Running
  • Clients log PollNetMap: ... unexpected EOF every 60 s (reverse proxy read timeout). With the proxy timeout raised to 3600 s, they log control: map response long-poll timed out! every 120 s instead, and the request is queued again
  • CLI calls exceed the 5 s deadline (DeadlineExceeded); headscale nodes rename took ~15 s

Reproduced in isolation: a local headscale 0.29.4 on a copy of the database, no reverse proxy, a single client registered as the router's user. One core is busy at 100 %, one map was eventually delivered after 96 s (bytes=366760 elapsed=96213), the next request ended with bytes=0 after 157 s because the client gave up at 120 s.

On 0.26.1, same data, same router: the netmap arrives within 1 second of registration, and a 30 s CPU profile shows 1.5 s of CPU in total.

CPU profile on 0.29.4 (production, 6 s, 26 s of samples) — 89 % of the time is in:

mapper.(*Batcher).worker
 mapper.generateMapResponse
  mapper.(*MapResponseBuilder).buildTailPeers
   types.NodeView.TailNode
    state.(*State).RoutesForPeer
     policy/v2.(*PolicyManager).ViaRoutesForPeer     23.27s  89.36%
      policy/v2.(*Group).Resolve                     23.11s  88.75%
       policy/v2.(*Username).resolve                 21.75s  83.53%
        policy/v2.(*Username).resolveUser            14.40s  55.30%

Goroutine dump at the same time: 106 goroutines waiting in mapper.(*Batcher).MapResponseFromChange (called from AddNode), 4 workers computing inside ViaRoutesForPeer, 2–3 workers waiting on a mutex in Batcher.worker.

What we think happens (reading v0.29.4)

Three factors multiply:

  1. buildTailPeers (hscontrol/mapper/builder.go) calls State.RoutesForPeer → ViaRoutesForPeer for every peer, including peers that advertise no route at all (only 3 of our 1026 nodes have routes).
  2. ViaRoutesForPeer (hscontrol/policy/v2/policy.go) converts every ACL to grants and resolves the sources and destinations of all of them before checking len(grant.Via) == 0. With no via grant in the policy, the result is always empty, so all that work is discarded. The function comment already says: "Hot callers should memoise by (policy-hash, viewer-id) rather than invoking this per-pair."
  3. aclToGrants (hscontrol/policy/v2/types.go) emits one grant per destination, each carrying the full source list. Our 50 ACL rules become 341 grants, so a rule with 67 destinations has its sources resolved 67 times.

Each Username.resolve walks all users and then all nodes, with no cache, and a username matching no user also builds a formatted error.

For our policy this gives 17 691 username resolutions per (viewer, peer) pair, i.e. about 30 million comparisons per pair and about 28.6 billion for one map with 946 peers, which matches the ~96 s we measured.

Related: #3346 (same symptoms after 0.28 → 0.29.1, on a ~200 device tailnet).

Expected Behavior

Netmap generation time on 0.29 should stay in the same order of magnitude as on 0.26 for a policy that does not use via.

Possible fixes, from cheapest to most complete:

  1. In ViaRoutesForPeer, return early when no grant in the policy has a via tag (ACL-derived grants never have one). This alone removes the cost for every policy without via.
  2. In RoutesForPeer / buildTailPeers, skip the via computation for peers that advertise no subnet or exit route.
  3. Resolve grant sources and destinations once per policy change (or memoise per policy hash and viewer), instead of once per (viewer, peer) pair.
  4. When converting ACLs with aclToGrants for this purpose, keep one grant per rule instead of one per destination.

We are happy to test a patch on a copy of our database.

Steps To Reproduce

  1. Use a tailnet with about 1000 nodes and several hundred users, and a policy made of acls rules only, with large groups as sources and many destinations per rule (ours: 50 rules, 341 destinations, 54 groups, 2056 usernames). No grants, no via.
  2. Make sure at least one node can see most of the tailnet as peers (in our case a subnet router reachable by everyone: 946 peers).
  3. Run headscale 0.29.4 and connect that node.
  4. Watch /machine/map for that node: the first map takes about 96 s on an otherwise idle server with a single client, and is never delivered when other clients are connected. CPU stays at one full core per busy worker.

Workaround we are evaluating (computed, not yet fully measured): rewriting the acls rules as native grants with all destinations in one grant ("ip": ["*"]) brings the number of source resolutions per pair from 341 down to 52, and removing usernames that match no user brings username resolutions from 17 691 down to 1 025. The compiled filter (/debug/filter) is byte-for-byte identical to the one produced by the original acls policy.

Environment

markdown
- OS: Kubernetes (GKE), official container image
- Headscale version: 0.29.4 (also went through 0.27.1 and 0.28.0; rolled back to 0.26.1)
- Tailscale version: 1.84.2 and 1.102.5 on the subnet routers (containerboot), 1.80 to 1.102 on clients
- Database: PostgreSQL 17
- Policy mode: database

Runtime environment

  • Headscale is behind a (reverse) proxy
  • Headscale runs in a container

Debug information

We cannot share the database, the policy or client netmaps as they are: they contain real user and customer names. We can produce an anonymised policy with the same shape and the same counts if that helps, and we can test a patch on a copy of the database.

Policy shape (mode database)

Section Count
acls rules 50, all accept, none with proto
destinations 341 in total (335 with port *), mostly hosts aliases, 34 tags
sources 55 groups, 16 tags, 7 usernames
groups 54, listing 2056 usernames (1585 distinct), 1189 of which match no user
hosts 65
tagOwners 22
grants, ssh, autoApprovers, autogroup:*, via none

headscale policy check accepts this policy on 0.27.1, 0.28.0 and 0.29.4.

Headscale configuration (environment variables, secrets and URLs removed)

HEADSCALE_POLICY_MODE=database
HEADSCALE_DATABASE_TYPE=postgres
HEADSCALE_PREFIXES_V4=100.64.0.0/10
HEADSCALE_PREFIXES_V6=fd7a:115c:a1e0::/48
HEADSCALE_DNS_MAGIC_DNS=true
HEADSCALE_DERP_URLS=https://controlplane.tailscale.com/derpmap/default
HEADSCALE_DERP_SERVER_ENABLED=false
HEADSCALE_LOG_LEVEL=info
oidc: enabled, email_verified_required: false
tuning: defaults (we also tried node_store_batch_size=1000, node_store_batch_timeout=5000ms, batch_change_delay=10s, batcher_workers=4, without restoring service)

Resource usage and timings by version (same tailnet, same evening, Prometheus)

0.26.1 0.27.1 0.28.0 0.29.4
CPU, average (cores) 0.33 1.24 1.37 3.52
CPU, max (cores) 1.03 1.33 1.41 9.47
headscale_nodestore_peers_calculation_duration_seconds, average n/a 9.4 s 7.6 s 13.0 s
subnet routers available 2 2 2 0
first netmap for a router with 946 peers ≤ 1 s not measured not measured never within 120 s

Router log on 0.29.4 (Tailscale 1.102.5, TS_BOOT_TIMEOUT=4m, reverse proxy timeout raised to 3600 s)

19:00:54 control: RegisterReq: got response; nodeKeyExpired=false, machineAuthorized=true; authURL=false
19:00:54 health(warnable=login-state): ok
19:02:54 control: map response long-poll timed out!
19:02:54 Received error: PollNetMap: Post "https://<server>/machine/map": context canceled

Headscale log on 0.29.4 (isolated reproduction, single client)

INF http request bytes=366760 elapsed=96213.596872 method=POST path=/machine/map proto=HTTP/2.0 status=200
INF http request bytes=0 elapsed=157059.834707 method=POST path=/machine/map proto=HTTP/2.0 status=200
ERR cannot write update to client error="http2: stream closed" component=poll

Goroutine dump on 0.29.4 (production, /debug/pprof/goroutine?debug=1, 370 goroutines)

106  mapper.(*Batcher).MapResponseFromChange < mapper.(*Batcher).AddNode < (*mapSession).serveLongPoll
  3  policy/v2.(*Username).resolveUser < (*Username).resolve < (*Group).resolve < (*PolicyManager).ViaRoutesForPeer < (*State).RoutesForPeer
  1  types.(*Node).AppendToIPSet < (*Username).resolve < (*Group).resolve < (*PolicyManager).ViaRoutesForPeer
  3  sync.(*Mutex).Lock < mapper.(*Batcher).worker

The full CPU profiles (0.26.1 and 0.29.4) are available on request.