<!-- XE-13227 · metrics,node: Handler runs preScrape before MaxRequestsInFlight and unsynchronized gauge updates tear /metrics readiness snapshots · testnet -->
*Markdown view of <https://bounty.xe.network/XE-13227>. The page is the record; this is the same page for reading.*

XE-13227 · Bounty

# metrics,node: Handler runs preScrape before MaxRequestsInFlight and unsynchronized gauge updates tear /metrics readiness snapshots

Closed · invalid
—
[xe#352](https://github.com/xeprotocol/xe/issues/352)
filed 2026-11-11 04:57 UTC
by Khanhtnq

Verdict
:   Invalid

Tier
:   —

Claimed
:   —

Area
:   Node / API

Duplicate of
:   —

Tracked
:   —

Updated
:   2026-11-20 00:25 UTC

Told
:   2026-11-20 00:24 UTC

### Patches

- [xe#353](https://github.com/xeprotocol/xe/pull/353) open by Khanhtnq
  fix(metrics,node): gate preScrape concurrency and serialize readiness scrape snapshots

### History

- [548810](https://bounty.xe.network/log/548810) 2026-11-20 00:24 UTC reply.mirrored
- [548709](https://bounty.xe.network/log/548709) 2026-11-20 00:24 UTC reply.sent
- [544652](https://bounty.xe.network/log/544652) 2026-11-20 00:09 UTC github.state\_pushed
- [544650](https://bounty.xe.network/log/544650) 2026-11-20 00:09 UTC github.labels
- [544497](https://bounty.xe.network/log/544497) 2026-11-20 00:08 UTC proposal.adopted
- [544437](https://bounty.xe.network/log/544437) 2026-11-20 00:08 UTC proposal.adopted
- [544436](https://bounty.xe.network/log/544436) 2026-11-20 00:08 UTC ticket.status new → closed
- [544316](https://bounty.xe.network/log/544316) 2026-11-20 00:08 UTC proposal.adopted
- [544315](https://bounty.xe.network/log/544315) 2026-11-20 00:08 UTC ticket.facts area
- [544312](https://bounty.xe.network/log/544312) 2026-11-20 00:08 UTC proposal.adopted
- [544311](https://bounty.xe.network/log/544311) 2026-11-20 00:08 UTC ticket.facts
- [536609](https://bounty.xe.network/log/536609) 2026-11-19 23:42 UTC note.added

[Full history](https://bounty.xe.network/log?ref=XE-13227) · [JSON](https://bounty.xe.network/v1/tickets/XE-13227) · [markdown](https://bounty.xe.network/XE-13227.md)

### Report

### Summary

`metrics.Handler()` and `node` observability have two coupled concurrency and resource-amplification bugs on `/metrics` (mounted on `OpsMux` in `api/observability.go:58-70` at `:60`, which intentionally bypasses HTTP rate limiting):

1. **`preScrape` runs outside `MaxRequestsInFlight: 4` and evaluates libp2p peer slices twice per scrape**:

   - In `metrics/metrics.go:159-178`, `Handler()` invokes `(*fn)()` (`o.publishDerived`) *before* `promhttp.HandlerFor(..., MaxRequestsInFlight: 4)` enforces the 4-request concurrency cap.
   - Every excess concurrent `/metrics` request that `promhttp` immediately sheds with `503 Service Unavailable` still executes `o.publishDerived()`, which calls `o.peerCount()` twice (`node/observability.go:331` and `o.ready()` at `node/observability.go:338 -> 468`), locking libp2p's swarm mutex and allocating peer slices twice per rejected request.
2. **Torn multi-metric readiness snapshots between `o.publish`, `o.publishDerived`, and `Registry.Gather()`**:

   - `o.sample()` -\> `o.publish(s)` (`node/observability.go:283-315`), `o.publishDerived()` (`node/observability.go:317-356`), and `Registry.Gather()` mutate and read 20\+ interrelated gauges (`xe_ready`, `xe_ready_check{check=...}`, `xe_ready_check_evaluated{check=...}`, `xe_healthy`, `xe_peer_count`) without mutual exclusion.
   - When `o.publish(s)` updates gauges concurrently with an active `/metrics` scrape, `Registry.Gather()` can emit contradictory readiness states in a single scrape payload (e.g. `xe_ready 1` alongside `xe_ready_check{check=...} 0`).

### Locations (`origin/master` @ `89a542ea`)

- `metrics/metrics.go:159-178`
- `node/observability.go:122-134, 167-173, 283-356, 442-471`
- `api/observability.go:58-70`

### Thread

**XE desk**team2026-11-20 00:24 UTCgithub

Thanks for the report. We read it against the code and could not confirm a bug in what it describes.

This report is closed as invalid. If we have missed something, file a new report that links
https://bounty.xe.network/XE-13227 and says what; it is read like any other.
