Skip to content

fix(process): avoid Gateway fork stalls in detached commands - #198478

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-micro-main-thread-spawn-via-broker-20260919
Nov 13, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-micro-main-thread-spawn-via-broker-20260919

Conversation

@steipete

@steipete steipete commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes Gateway pauses when detached callbacks start Git, exec-tool commands, or container helpers from a large Node process.

User Impact

Linux Gateway commands continue using the existing small spawn broker after their request async context has ended. Command output, exit codes, environment, working directory, cancellation, deadlines, and process-tree ownership keep their existing contracts.

Why This Change Was Made

The broker belongs to the Gateway lifetime, but its lookup only consulted request async context. Both shared spawn adapters therefore fell back to native spawn for detached work even when the broker was healthy. Register the ready broker at its existing owner and clear it after successful host cleanup. Explicit native-only scopes and scoped broker overrides retain precedence.

Production growth is 14 lines for the lifetime registration and cleanup hook. No configuration, cache, schema, or duplicate command implementation. Native transport remains for CLI/other runtimes, host-owned descriptors and explicit synchronous launch-authority boundaries.

Evidence

Both regressions fail on exact parent 649ca5f879f1e4349a2c5cf9872e30366962a595: detached commands have the main process as parent, and the real exec fixture completes without using its ready broker. Candidate owner, exec, transport and Gateway shutdown coverage passes; the lifecycle batch is 62 tests. The new owner test takes about 1.1 seconds of test time; compilation is excluded from that figure.

Codex autoreview of the complete local candidate is scoped-clean through P2, with no accepted/actionable findings. Core test typechecks and scoped script checks pass after fixture corrections. pnpm check:changed passes every gate except unused exports; those failures reproduce unchanged on a clean exact-parent checkout:

These are recorded main regressions, not passing checks.

Linux proof uses the committed scripts/perf/durable-spawn-measure.ts workload, source-frozen parent/candidate bundles, a 9.11 GiB resident parent including 4 GiB V8 heap, five alternating pairs and identical-source A/A controls (250 commands per source per owner). All 750 native host spawns become zero.

Owner Native blocking median / p95, ms Main-thread CPU median, ms/command Wall median / p95, ms
Buffered Git 39.803 / 97.065 → 0 / 0 23.924 → 0.610 42.750 / 116.405 → 2.362 / 25.603
Guarded exec 32.002 / 50.819 → 0 / 0 18.073 → 0.586 32.438 / 51.288 → 2.358 / 14.410
Container helper 32.648 / 59.934 → 0 / 0 19.458 → 0.677 46.529 / 101.649 → 14.007 / 44.034

Median allocation per command: Git 514,667 → 342,058 bytes; guarded exec 120,356 → 187,833 bytes; container 747,066 → 360,073 bytes. The guarded path adds about 66 KiB (+56%) through existing broker admission/IPC while reducing main-thread CPU by 96.8%. A/A wall medians were 42.324/42.856 → 2.318/2.429 ms for Git, 32.461/32.416 → 2.394/2.336 for guarded exec, and 46.009/46.917 → 14.059/13.976 for container helpers.

Separate CPU profiles (50 commands/source/owner) attribute 1,396 ms of Git and 1,503 ms of guarded-exec samples to native spawn before, with no native-spawn samples after. Timing acceptance uses unprofiled runs.

This is transport-owner proof in an isolated local Linux VM (ARM64, Node 26.12.1, overlayfs), not complete Gateway throughput or btrfs coverage. The commands do not read the database or workspace; the synthetic fixture matches resident heap and mapping cost without copying production data. The broker remains about 80–84 MiB RSS.

@clawsweeper

clawsweeper Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime scripts Repository scripts agents Agent runtime and tooling size: M labels Nov 13, 2026
@steipete
steipete marked this pull request as ready for review November 13, 2026 22:10
@chatgpt-codex-connector

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

Copy link
Copy Markdown
Contributor
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T22:13:46.866417Z df3ac4a Draft marked ready
ℹ️ 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.

@steipete

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

The branch and evidence are complete at df3ac4a. Please review the Gateway lifetime broker lookup, explicit native-only scope precedence, and close cleanup. The PR body includes red-on-parent regressions, final Linux measurements at 9.11 GiB RSS, A/A controls, CPU profiles, and the guarded-path allocation tradeoff.

@clawsweeper

clawsweeper Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 13, 2026
@clawsweeper

clawsweeper Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR makes detached Gateway commands use the existing spawn broker while preserving scoped overrides and explicit native-only execution.

Example: A detached callback runs Git from a Linux Node Gateway with 9.11 GiB RSS.

  • Before: The command forks the large Gateway process; the supplied measurement reports 39.803 ms median native blocking.
  • After: The command uses the small broker; the supplied measurement reports zero native Gateway spawns.

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) The implementation is focused and source-consistent, with meaningful process-level measurements and regression coverage.
Proof confidence 🦪 silver shellfish (2/6) Real behavior proof is not required for maintainer- or bot-authored pull requests.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Detached command launches can briefly freeze a large Linux Gateway despite its healthy spawn broker.
Reason: The reported source failure is concrete, the direction is maintainer-approved, and the change reuses the existing process owner without new user configuration.

Merge readiness

✅ Ready for maintainer review

Keep open: current main still loses the broker outside request context, and this focused, maintainer-directed repair addresses that gap without an identified correctness regression.

Priority: P2
Reviewed head: df3ac4a1523cd62d21336add84048466c9bb30c4

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: Provide redacted logs or terminal output from an isolated Linux Gateway showing a command launched after request-context detachment uses its broker and completes normally.
Agent review details

How this fits together

Gateway startup owns the broker; shared command adapters select it to launch child processes while existing supervisors retain output, cancellation, authority and cleanup.

flowchart TD
  A[Gateway startup] --> B[Ready broker lifetime]
  C[Detached callback] --> D[Transport lookup]
  E[Explicit scope] --> D
  B --> D
  D --> F[Broker or native launch]
  F --> G[Existing process supervisor]
Loading

Technical review

Best possible solution:

Keep the focused lifetime registration in the existing broker owner, preserving explicit transport scopes and successful-cleanup invalidation.

Do we have a high-confidence way to reproduce the issue?

Current-main lookup and both shared launch adapters establish the native fallback after detachment; the author also reports regressions failing on the exact parent, which this read-only review did not execute.

Is this the best way to solve the issue?

The patch fixes transport discovery at its existing owner with a small lifecycle registration, and AsyncContext already preserves the removed redundant same-value fast path.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against b74fa8348ffc.

Provenance checked

  • Broker lookup lifetime changes intended behavior with a stated reason (67cedc3: The original broker avoided Linux page-table copying while keeping process supervision and launch contracts with existing owners.)
  • Same-scope execution optimization keeps the original intent (5563f0d: Avoid redundant async-context frames; the current AsyncContext.run implementation already skips identical values.)
  • Scheduled exec integration coverage keeps the original intent (d940d94: Verify real command completion and broker use without assuming the shell is its immediate child under Linux memory protection.)
  • Broker shutdown diagnostics and settlement keeps the original intent (fix(process): keep the spawn broker's shutdown error #170829: Preserve actual broker shutdown failures and cleanup ownership; the added callback follows successful settlement without suppressing errors.)
  • Detached-work documentation and measurement workload keeps the original intent (954f1a4: Measure native versus broker fork cost through real command owners; this PR extends the workload to source-specific lifetime lookup.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 67cedc3 adds src/process/spawn-broker/context.ts:40 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 67cedc32e996; files: src/process/spawn-broker/context.ts)
  • Shakker: Raw commit d940d94 adds src/agents/bash-tools.exec-runtime.broker.test.ts:86 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: d940d9481401; files: src/agents/bash-tools.exec-runtime.broker.test.ts)

PR surface

Source +14, Tests +76, Docs 0, Other +52. Total +142 across 6 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 2 18 4 +14
Tests 2 90 14 +76
Docs 1 3 3 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 71 19 +52
Total 6 182 40 +142

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Labels

Label changes:

  • add P2: Detached command launches can pause a large Linux Gateway, but the evidence does not establish a core outage or urgent released regression.
  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P2: Detached command launches can pause a large Linux Gateway, but the evidence does not establish a core outage or urgent released regression.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed November 13, 2026, 5:15 PM ET / 22:15 UTC.

@steipete
steipete merged commit 2f6061c into main Nov 13, 2026
220 of 225 checks passed
@steipete
steipete deleted the steipete/perf-micro-main-thread-spawn-via-broker-20260919 branch November 13, 2026 22:25
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. docs Improvements or additions to documentation gateway Gateway runtime scripts Repository scripts size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant