Skip to content

perf(diagnostics): avoid Gateway stalls when starting CPU profiles - #170932

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-micro-cpuprofile-start-stall-20260919
Oct 13, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-micro-cpuprofile-start-stall-20260919

Conversation

@steipete

@steipete steipete commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Starting diagnostics.cpuProfile stalls every Gateway user while a fresh inspector profiler scans V8's heap, even though the Gateway's stall flight recorder already has an active native sampler and code map.

User Impact

On Node 26 with the stall flight recorder active, operator CPU captures join the existing native profiler and start without another heap scan. The five-second capture, 10 ms sampling interval, admin authorization, cancellation, cleanup, redaction and 1 MiB response limit remain in place. Node captures without an active native profile can still scan the heap; Bun retains its inspector backend.

Why This Change Was Made

A retained inspector Session does not retain V8's profiler after Profiler.stop. Node's v8.startCpuProfile() instead shares the runtime-owned profiler used by the recorder's overlapping windows. The shared capture owner now handles both native and inspector lifetimes, with all heap-profile and heap-snapshot callers migrated together. Native JSON decoding and numeric script-ID conversion have one shared implementation used by the RPC and recorder.

Production LOC increases by 75 lines. The additional backend lifetime contract preserves the shipped Bun CPU, heap sampling and heap snapshot paths while keeping admission, authority and cleanup in one owner; it avoids duplicating those policies in a separate Node capture implementation. No new dependency, configuration option, persistent profiler or schema change.

Evidence

An isolated Linux sandbox on Node 26.8.2 ran the real dist Gateway, warmed health, status, sessions.list, models.list and diagnostics.lanes, and retained approximately 3.46 GB of synthetic objects. Three repeated RPC captures per variant:

Capture path startBlockedMs
Before: fresh inspector profiler 1365.703, 1292.001, 1288.714
Retained inspector Session control 1419.667, 1411.273, 1318.555
After: shared native profiler 0.044157, 0.019880, 0.020060

All completed captures returned valid sanitized graphs and samples. Enable/interval setup was below 0.3 ms; the repeated heap scan dominated startup. Standalone native profiling without an active recorder still took about a second on a 3.2 GB synthetic heap, establishing why sharing the already-active native profiler matters.

Validation: changed-file oxfmt and repo lint; focused CPU, heap, recorder, CPU RPC and heap-snapshot RPC tests with --maxWorkers=1 (101 tests passed across five focused files; the CPU test file took 9.74 s including its real five-second capture, and the latest two-file invocation took 42.50 s). Exact-head PR CI passed production/test typechecks, lint, ratchets, source and architecture guards, changed test lanes and selected UI E2E checks in 1,098 s elapsed (including queue time). The merged head is 66022a6ba9ee64a026113650e96cf32c7f1c0612. No Testbox/Crabbox leases. independent subagent review (autoreview route unavailable); no actionable P1/P2 findings.

The initial CI run identified a missing SAFETY invariant comment on the extracted native JSON assertion. The reviewed follow-up documents the V8-owned input shape without changing behavior; CPU and recorder tests were repeated (56 passed, 59.40 s wall time including compiler preparation).

Release-note context: operator CPU profiling on Node reuses the Gateway's existing native sampler to avoid capture-induced main-thread stalls.

@clawsweeper

clawsweeper Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T10:26:10.905944Z 66022a6 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime size: M labels Oct 13, 2026
@steipete
steipete merged commit ab21d37 into main Oct 13, 2026
118 of 119 checks passed
@steipete
steipete deleted the steipete/perf-micro-cpuprofile-start-stall-20260919 branch October 13, 2026 10:42
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 14, 2026
…penclaw#170932)

* perf(diagnostics): reuse native CPU profiler for operator captures

* fix(diagnostics): document native profile decoding invariant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation gateway Gateway runtime size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant