Skip to content

inherited_children: reap inherited children on macOS too - #2234

Open
zorahrel wants to merge 1 commit into
1jehuang:masterfrom
zorahrel:macos-reap-inherited-children
Open

zorahrel wants to merge 1 commit into
1jehuang:masterfrom
zorahrel:macos-reap-inherited-children

Conversation

@zorahrel

Copy link
Copy Markdown

Closes #2233

Problem

inherited_children only had a Linux implementation (it reads procfs), so on macOS start() was a no-op. A reloaded shared server never reaped the children it inherited and never killed stale MCP servers. A jcode serve daemon on macOS 26 up for 35 hours carried 10 zombies, timed to its reloads.

Change

  • mod imp and start() are enabled on macOS as well as Linux.
  • children_of on macOS uses proc_listpids(PROC_PPID_ONLY, pid, …). It walks the zombie list too, and it returns bytes (unlike proc_listchildpids, whose unit changed across releases). A little headroom covers children spawned between the size query and the read.
  • has_mcp_marker on macOS reads the environment of a same-user process with sysctl(KERN_PROCARGS2).
  • The reap loop and the Linux code are unchanged.
  • detects_mcp_marker used to spawn /bin/sleep. macOS hides the environment of SIP platform binaries from KERN_PROCARGS2, so the test now re-runs its own binary on an ignored helper test that only sleeps. Real MCP servers (node, npx, python) are readable, checked by hand.

Tests (macOS 26, Apple Silicon)

  • cargo test -p jcode-base inherited_children: 2 passed, 1 ignored (the helper).
  • Mutation: has_mcp_marker always false → detects_mcp_marker fails. children_of returning nothing → detects_mcp_marker fails at the children_of assertion.
  • cargo fmt --check is clean for this file. cargo clippy -p jcode-base --tests reports nothing in it; it does stop on two existing errors in mobile_runner_events_tests.rs and runtime_activity.rs, unrelated to this change.
  • Not run locally on Linux: there the code path is unchanged, apart from the test helper, which uses only std and is left to CI.

The module was Linux-only because it reads procfs. On macOS start() was a
no-op, so children a reloaded server inherited were never reaped and stale
MCP servers were never killed: a shared server up for 35 hours carried 10
zombies, timed to its reloads.

macOS lists children with proc_listpids(PROC_PPID_ONLY), which walks the
zombie list too, and reads the environment with sysctl(KERN_PROCARGS2).
The reap loop is shared.

The marker test spawned /bin/sleep, whose environment macOS hides from
KERN_PROCARGS2 (SIP platform binary). It now re-runs the test binary on an
ignored helper test that only sleeps.

Closes 1jehuang#2233
@greptile-apps

greptile-apps Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change reaches macOS process-start and daemon-reload cleanup.

Fix the marker check before merging so reload cannot kill an unmarked child based on its arguments.

Findings

  1. P1 Arguments can trigger unwanted kills ▶
  2. P2 Zombie discovery is not tested ▶
Fix with agent prompt
### Issue 1
crates/jcode-base/src/inherited_children.rs:132-135
**Arguments can trigger unwanted kills.** `KERN_PROCARGS2` includes command arguments as well as the environment, but this scans both. If a still-running custom hook receives a literal argument beginning with `JCODE_MCP_CHILD=`, reload treats it as an MCP server and kills it and its process group, even without the environment marker. Skip the executable path and the recorded number of arguments before checking environment entries.

### Issue 2
crates/jcode-base/src/inherited_children.rs:199
**Zombie discovery is not tested.** This calls `children_of` only after `reap` has removed the exited child. The other test checks only a live child. An implementation that lists live children but misses zombies would pass both tests while leaving the macOS bug unfixed. Assert that the exited, unreaped child appears in `children_of` before calling `reap`, then keep the absence check afterward.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Adds macOS support for cleaning up children left behind by a reload.

  • macOS startup now cleans up children left by a reload.

The marker check can mistake command arguments for environment entries. The tests also miss the key case of finding an exited child before reaping it.

Reviews (1) · Last reviewed commit: "inherited_children: reap inherited child..." · Reviewed by Greptile

Comment on lines +132 to +135
buf.truncate(len);
let needle = format!("{}=", super::MCP_CHILD_MARKER_ENV);
buf.split(|b| *b == 0)
.any(|entry| entry.starts_with(needle.as_bytes()))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Arguments can trigger unwanted kills. KERN_PROCARGS2 includes command arguments as well as the environment, but this scans both. If a still-running custom hook receives a literal argument beginning with JCODE_MCP_CHILD=, reload treats it as an MCP server and kills it and its process group, even without the environment marker. Skip the executable path and the recorded number of arguments before checking environment entries.

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-base/src/inherited_children.rs
Line: 132-135

Comment:
**Arguments can trigger unwanted kills.** `KERN_PROCARGS2` includes command arguments as well as the environment, but this scans both. If a still-running custom hook receives a literal argument beginning with `JCODE_MCP_CHILD=`, reload treats it as an MCP server and kills it and its process group, even without the environment marker. Skip the executable path and the recorded number of arguments before checking environment entries.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

super::reap(&mut set);
assert!(set.is_empty());
assert!(!std::path::Path::new(&format!("/proc/{pid}")).exists());
assert!(!super::children_of(std::process::id()).contains(&pid));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Zombie discovery is not tested. This calls children_of only after reap has removed the exited child. The other test checks only a live child. An implementation that lists live children but misses zombies would pass both tests while leaving the macOS bug unfixed. Assert that the exited, unreaped child appears in children_of before calling reap, then keep the absence check afterward.

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-base/src/inherited_children.rs
Line: 199

Comment:
**Zombie discovery is not tested.** This calls `children_of` only after `reap` has removed the exited child. The other test checks only a live child. An implementation that lists live children but misses zombies would pass both tests while leaving the macOS bug unfixed. Assert that the exited, unreaped child appears in `children_of` before calling `reap`, then keep the absence check afterward.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

xenking added a commit to xenking/jcode that referenced this pull request Nov 22, 2026
Snapshot Darwin children, including zombies, before this process image spawns new work. Reuse the existing reaper and identify stale MCP children from their environment.

Upstream: 1jehuang#2234

Co-authored-by: Attilio Cianci <[email protected]>
xenking added a commit to xenking/jcode that referenced this pull request Nov 22, 2026
Upstream-PR: 1jehuang#1357
Upstream-PR-Commit: 1jehuang#1357 0da5e81
Upstream-PR: 1jehuang#1362
Upstream-PR-Commit: 1jehuang#1362 5962058
Upstream-PR: 1jehuang#1486
Upstream-PR-Commit: 1jehuang#1486 fadc624
Upstream-PR: 1jehuang#1733
Upstream-PR-Commit: 1jehuang#1733 01f3415
Upstream-PR: 1jehuang#1750
Upstream-PR-Commit: 1jehuang#1750 1ac0c82
Upstream-PR: 1jehuang#1822
Upstream-PR-Commit: 1jehuang#1822 73f85fc
Upstream-PR: 1jehuang#1912
Upstream-PR-Commit: 1jehuang#1912 36d7682
Upstream-PR: 1jehuang#1978
Upstream-PR-Commit: 1jehuang#1978 0f55643
Upstream-PR: 1jehuang#2007
Upstream-PR-Commit: 1jehuang#2007 638f87c
Upstream-PR: 1jehuang#2213
Upstream-PR-Commit: 1jehuang#2213 c5e1487
Upstream-PR: 1jehuang#2234
Upstream-PR-Commit: 1jehuang#2234 0d4d5d4
Upstream-PR: 1jehuang#2266
Upstream-PR-Commit: 1jehuang#2266 217ddc2
Upstream-PR: 1jehuang#2267
Upstream-PR-Commit: 1jehuang#2267 e98c40b

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

macOS: the shared server never reaps children inherited across a reload

1 participant