Skip to content

perf(serve): cache the /api/status profile-gateway topology scan (salvage #71396) - #76969

Merged
kshitijk4poor merged 1 commit into
NousResearch:mainfrom
kshitijk4poor:salvage-71396-topology-cache
Aug 2, 2026
Merged

perf(serve): cache the /api/status profile-gateway topology scan (salvage #71396)#76969
kshitijk4poor merged 1 commit into
NousResearch:mainfrom
kshitijk4poor:salvage-71396-topology-cache

Conversation

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Salvages #71396 by @lost9999 — commit cherry-picked to preserve authorship. Merges clean; no changes to the contribution.

Context — honest premise update

The PR was written against a desktop-boot GIL-starvation symptom: the desktop polled /api/status ~1/s during boot, each uncached _collect_profile_gateway_topology call walks 7+ profile homes (yaml.safe_load + psutil + realpath) in the executor, and concurrent polls piled up holding the GIL for 14-16s, starving gateway.ready. Since then, 23cb26c moved desktop boot readiness to /api/health, so the boot symptom itself no longer occurs on current desktops.

The cache still carries real value, which is why we're landing it: /api/status remains polled by the web sidebar every 10s, the statusbar every 60s, Hermes Cloud Portal, multiple desktop windows, and the legacy boot-fallback for pre-0.19.0 backends. The topology scan is genuinely expensive and identical across concurrent callers — a 10s TTL with a collapse lock deduplicates all of it.

What the fix does

10s-TTL double-checked-locking cache around the topology scan, with a function-identity guard that makes monkeypatched collectors an automatic cache miss (test hermeticity without reset hooks).

Verification

  • The PR's 4 cache tests + 11 topology tests: 15 passed on current main; full test_web_server.py 120 passed / 1 skipped in the reviewer's merged-tree run
  • Overlap re-checked against the recently merged fix(dashboard): preserve maintenance writes on read polling (salvage #67903) #76895 web_server.py changes: hunks at ~2900-3250 vs ~11055-11169 — disjoint
  • Seam sweep: all existing monkeypatch seams keep working via the fn-identity miss
  • Reviewer note for the future (non-blocking): two HTTP-level tests using the REAL collector within 10s of each other would share cache state; nothing on main does that today

Closes #71396 (superseded by this salvage — original author credited via cherry-pick authorship).

/api/status is the desktop's boot liveness probe (polled ~1/s) but since
NousResearch#60537 every call ran a full topology scan — per-profile yaml.safe_load
(pure-Python loader), psutil process probes, realpath walks — in the
default executor. On multi-profile installs concurrent polls pile up and
hold the GIL 14-16s, starving the event loop: the WS sidecar cannot
flush gateway.ready, the desktop times out into the next stall, and boot
escalates to the 'Hermes couldn't start' overlay (NousResearch#60800).

Memoize the scan behind a 10s TTL with a collapse lock so concurrent
polls share one scan. Topology only changes on gateway start/stop, so a
<=10s stale badge is an acceptable trade for not starving the loop. The
cache also keys on the collector's identity: tests monkeypatch
_collect_profile_gateway_topology per case, and the identity check keeps
them hermetic (a swapped collector is a miss) without a reset hook.

py-spy captures during a failing boot land in _profile_platform_ports ->
yaml.safe_load on executor threads (7 profiles, Windows). After: one
cold-start scan, zero recurring stalls, desktop boots.
@kshitijk4poor
kshitijk4poor enabled auto-merge (rebase) August 2, 2026 17:37
@kshitijk4poor
kshitijk4poor merged commit 47fa438 into NousResearch:main Aug 2, 2026
35 checks passed
@alt-glitch alt-glitch added type/perf Performance improvement or optimization P3 Low — cosmetic, nice to have comp/cli CLI entry point, hermes_cli/, setup wizard comp/dashboard Web dashboard / control panel UI (dashboard/, landing) labels Aug 2, 2026
@kshitijk4poor
kshitijk4poor deleted the salvage-71396-topology-cache branch August 5, 2026 07:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/cli CLI entry point, hermes_cli/, setup wizard comp/dashboard Web dashboard / control panel UI (dashboard/, landing) P3 Low — cosmetic, nice to have type/perf Performance improvement or optimization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants