#5682·pyroscope

metastore/discovery: kuberesolver can keep a stale service account token, causing persistent 401s that leave clients dialing old metastore IPs

Author: YaronFrCreated Sep 24, 2026Updated Sep 24, 2026

Summary

With the Helm chart's default -metastore.address=kubernetes:///... (operations/pyroscope/helm/pyroscope/templates/deployments-statefulsets.yaml:94, from #4417), metastore discovery uses the kuberesolver copy in pkg/metastore/discovery/kuberesolver (added in #3540). It reads the service account token once at startup and then re-reads it only on fsnotify events (kubernetes.go:106-136). Every failure on that path is discarded: the watcher.Add return value, a failed os.ReadFile, and anything sent on watcher.Errors:

go
if event.Op.Has(fsnotify.Remove) || event.Op.Has(fsnotify.Chmod) {
	// remove watcher since the file is removed
	watcher.Remove(event.Name)
	// add a new watcher pointing to the new symlink/file
	watcher.Add(serviceAccountToken)
	token, err := os.ReadFile(serviceAccountToken)
	if err == nil {
		client.setToken(string(token))
	}
}
...
case _, ok := <-watcher.Errors:
	if !ok {
		return
	}

There is no fallback re-read, so if events stop arriving the in-memory token is never replaced. Once the API server rejects the token, the endpoints watch fails on every retry (builder.go:41-46, 1 s to 30 s backoff, same token), and the resolver keeps the last endpoint list it saw:

component=kuberesolver-discovery msg="watching ended with error, will reconnect again" err="invalid response code 401 for service pyroscope-metastore-headless in namespace pyroscope"

KubeDiscovery.Rediscover() is empty (pkg/metastore/discovery/kuberesolver.go:29, from #3606), so the metastore client's call to it after failed attempts (pkg/metastore/client/methods.go:85) has no effect. This is secondary: re-listing with the same token would also get a 401.

We could not establish whether the watch was lost, an event was missed, or a read failed. All three are discarded, so the pod logs nothing either way.

Impact

  • The stale endpoint list is harmless until metastore endpoints change. After that, the affected pod keeps dialing the old pod IPs (dial tcp <old pod IP>:9095: i/o timeout) until it is restarted.
  • A segment-writer in this state fails AddBlock on every segment, each call only after the 2 s metadata update timeout, and every block goes through the DLQ.
  • A metastore follower in this state stays NotReady.
  • We saw 6 affected pod instances (metastore and segment-writer) on two clusters within about a week. Only a restart cleared it; the longest case lasted about 4 days.

Timeline on the first cluster (UTC):

Time Event
2026-09-17 09:24 A metastore follower and a segment-writer start on the same node
2026-09-19 08:24 The follower starts getting 401s (about 47 h after start)
2026-09-19 19:54 The segment-writer starts getting 401s (about 58 h after start)
2026-09-22 18:52 The other metastore pods are rescheduled and get new IPs. Both affected pods keep dialing the old IPs
2026-09-22 18:52 to 2026-09-23 16:47 The segment-writer's AddBlock fails on every segment (about one per second). The follower stays NotReady
2026-09-23 16:47 Both pods are restarted and the errors stop

On the second cluster, the EKS control plane audit log shows the token side:

  • A metastore pod kept sending a single token, issued around 2026-09-18 00:22 UTC. The issue time is inferred from the authentication.k8s.io/stale-token annotation: request time, minus the reported seconds past the warning threshold, minus 3607 s.
  • Every request carried that annotation, with growing lateness.
  • The API server rejected the token at 2026-09-19 00:23:53 UTC ("service account token has expired"), when it was about 24 h old.
  • The 401s continued until the pod was restarted 28 h later.

No other workload on that node sent a stale token in that window, which argues against a node-wide token rotation failure. We did not capture the affected pod's token file, so a pod-specific projection failure is not ruled out.

Reproduction

This reproduces intermittently, not on demand.

  1. Deploy with the Helm chart: v2 storage, microservices mode, default metastore.address. Observed on 2.3.1 (revision 7aeaa0f); the code above is unchanged on main.
  2. Leave it running. Some pods start logging the 401 line above; for us, 1 to 3 days after start.
  3. Delete the other metastore pods so they come back with new IPs. The affected pods keep dialing the old IPs until they are restarted.

Environment: Kubernetes (EKS 1.35), helm chart 2.3.1, image 2.3.1, microservices mode, v2 storage, projected service account token (expirationSeconds: 3607).

Suggested fix

  1. Refresh the token independently of fsnotify. For example, re-read the file whenever the cached copy is older than a short period, as client-go's transport.NewCachedFileTokenSource does (one-minute period). Log and retry read failures.
  2. Re-read the token and retry after a 401.
  3. Make KubeDiscovery.Rediscover() re-list endpoints.

The same token handling is in upstream sercand/kuberesolver v6.0.1, so it may be worth fixing there as well. A good regression test would replace the token file without delivering any fsnotify event, then assert that the client picks up the new token within the refresh period.

Our current workaround is DNS SRV discovery:

-metastore.address=dnssrvnoa+_grpc._tcp.pyroscope-metastore-headless.<namespace>.svc.cluster.local.:9095

With it, we moved a follower, moved the leader and replaced all three metastore pods. There were no failed pushes and no failed AddBlock calls.